引用
import java.io.*;
public class Father01{
int a =100;//Father类属性a
public void miner(){a--;}//Father类方法miner()
public static void main(String[] args){
Father01 f=new Father01();
Son01 s=new Son01();
System.out.println("a of son is:"+s.a);//子类属性s.a:0
System.out.println("a of father is:"+f.a);//父类属性f.a:100
System.out.println("a of son's super is:"+s.getA());//子类调用其方法返回父类的属性a:100
s.miner();//子类调用父类的方法miner(),自减操作,所以对象S实例出来的父类属性a=100-1=99
System.out.println("a of son is:"+s.a);//子类属性s.a:0
System.out.println("a of father is:"+f.a);//对像f的属性f.a.仍然没有改变:100
System.out.println("a of son's super is:"+s.getA());//因减1后,所以其值变为:99
s.plus();//子类属性a加1
System.out.println("a of son is:"+s.a);//子类属性s.a:1
}
}
class Son01 extends Father01{
int a=0;//这是Son01自定义的属性a,不能理解为从Father01类中继承下来的属性
public void plus(){a++;}
public int getA(){return super.a;}//super.a为父类Father01的属性
}
public class Father01{
int a =100;//Father类属性a
public void miner(){a--;}//Father类方法miner()
public static void main(String[] args){
Father01 f=new Father01();
Son01 s=new Son01();
System.out.println("a of son is:"+s.a);//子类属性s.a:0
System.out.println("a of father is:"+f.a);//父类属性f.a:100
System.out.println("a of son's super is:"+s.getA());//子类调用其方法返回父类的属性a:100
s.miner();//子类调用父类的方法miner(),自减操作,所以对象S实例出来的父类属性a=100-1=99
System.out.println("a of son is:"+s.a);//子类属性s.a:0
System.out.println("a of father is:"+f.a);//对像f的属性f.a.仍然没有改变:100
System.out.println("a of son's super is:"+s.getA());//因减1后,所以其值变为:99
s.plus();//子类属性a加1
System.out.println("a of son is:"+s.a);//子类属性s.a:1
}
}
class Son01 extends Father01{
int a=0;//这是Son01自定义的属性a,不能理解为从Father01类中继承下来的属性
public void plus(){a++;}
public int getA(){return super.a;}//super.a为父类Father01的属性
}