}
class Graduate extends Student { }
public class TestExtends_2 { } /* */
2009年5月31日14:59:26 本程序证明了:
1、 子类内部可以访问父类非私有的成员
2、 通过子类对象名只能访问从父类继承过来的非私有成员
私有成员无法被子类方法访问
public static void main(String[] args) { }
Graduate gd = new Graduate();
System.out.printf(\
public String daoshi = \小三\
总结:
私有不能被继承
私有物理上已经被继承过来,只不过逻辑上程序员不能去访问它 因此继承必须慎重,否则会浪费内存
class A { }
class B extends A {
private void m() {
i = 10; j = 20;
//k = 30; //error 私有属性不能被继承 g(); b();
public int i; protected int j; private int k;
public void g() { }
private void s() { }
protected void b() { }
}
}
//s(); // error 私有方法不能被继承
public void f() { }
i = 10; j = 20;
//k = 30; //error 私有属性不能被继承 g(); b();
//s(); // error 私有方法不能被继承
class M {
public static void main(String[] args) {
B bb = new B(); bb.i = 20; bb.j = 30; bb.b(); bb.g();
//bb.s(); //error //bb.k = 22;
}
}
class A { }
public class TestStatic_1 {
public static void main(String[] args) {
private int i;
private static int cnt = 0;
public A() { }
public A(int i) { }
public static int getCnt() { }
//return 返回的是A对象的个数; return cnt; this.i = i; ++cnt; ++cnt;
}
}
System.out.printf(\当前时刻A对象的个数是: %d\\n\A aa1 = new A();
System.out.printf(\当前时刻A对象的个数是: %d\\n\A aa2 = new A();
System.out.printf(\当前时刻A对象的个数是: %d\\n\
class A { }
public class TestStatic_2 {
public static void main(String[] args) {
A aa1 = A.getA(); A aa2 = A.getA();
public int i = 20;
private static A aa = new A(); //aa是否是A对象的属性
private A() { }
public static A getA() //static 一定是不能省略的 { }
return aa;
aa1.i = 99;
//
A()就会报错! // }
}
System.out.printf(\
if (aa1 == aa2) System.out.printf(\和 aa2相等\\n\
else System.out.printf(\和 aa2不相等\\n\
A aa1 = new A(); // error 如果A类的构造方法是private的,则new
A aa2 = new A(); //同上