仪器社区

java定义一个子类一个父类一个测试类

Cc春羽 2012-05-03
评论
全部评论
yao6184420
public class DD {
public static void main(String[] args) {
B1 b = new B1();
A1 a = b;
a.fun1();
a.fun2();
System.out.println(a instanceof B1);
}
}
class A1{
public void fun1(){
System.out.println("A--> public void fun1");
}
public void fun2(){
this.fun1();
}
}
class B1 extends A1{
public void fun1(){
System.out.println("B--> public void fun1");
}
public void fun3(){
System.out.println("B--> public void fun3");
}
}
2 0 2018-03-23 0条评论 回复
波诡云谲24
public class Fruit {
public String getName() {
return "Fruit";
}
}

public class Apple extends Fruit {
@Override
public String getName() {
return "Apple";
}
}

public class Test {
public static void main(String[] args) {
Fruit father = new Fruit();
Fruit son = new Apple();
Apple apple = new Apple();

System.out.println(father.getName()); // "Fruit"
System.out.println(son.getName()); // "Apple"
System.out.println(apple.getName()); // "Apple"
}
}
10 0 2012-05-04 0条评论 回复
您可能感兴趣的社区主题
加载中...
发布 评论