class Base{
String var1="This is the base";
static String var2="This is the base static";
static void say(){
System.out.println("base static is using");
}
void get(){
System.out.println("base is using");
}
}
public class Sub extends Base {
String var1="This is the sub";
static String var2="This is the sub static";
static void say(){
System.out.println("sub static is using");
}
void get(){
System.out.println("sub is using");
}
public static void main(String[] args){
Base bs=new Sub();
System.out.println(bs.var1);//打印base 类的属性
System.out.println(bs.var2);//打印base 类的属性
bs.say();//打印base 因为static声明的为类的实例
bs.get();//打印sub 对象的方法
}
}
多态的一个实例
最新推荐文章于 2024-03-18 21:39:26 发布