问一个也许很菜的问题!但是我想了很久没整明白~~!
public class w {
int a,b,c;
w(int a,int b,int c) {
this.a = a ;
this.b = b;
this.c = c;
}
public void diaplay() {
System.out.println( "a= "+ a + "b= "+ b + "c= "+ c);
}
}
class Test {
public static void main(String[] args) {
w d1 = new w(1,11,1111);
w d2 = new w(2,22,2222);
Test t =new Test();
d1.diaplay();
d2.diaplay();
t.diaplay();//为什么不能访问?
}
这里为什么对象t不能访问diaplay方法呢?为什么d1,d2都又可以呢?
这里面是个什么规则呢?望大家指教啊,谢谢了!
------解决方案--------------------d1,d2是class w的实例,
t是class Test的实例,
diaplay方法是class w的方法,
当然是d1,d2能访问,t不能访问了。
------解决方案--------------------用d1(d2).display()就可以调用了,调用是先指明实例,再写出要调用的方法,你在t里面没有display()这个方法,当然不行了。