java (1)定义抽象类People,包括两个属性:name,sex;一个抽象方法void sayHello();
发布网友
发布时间:2022-05-23 23:04
我来回答
共2个回答
热心网友
时间:2024-03-09 22:24
People.java:
package util;
public abstract class People {
private String name;
private String sex;
abstract void sayHello();
}
ChinaPeople.java:
package util;
public class ChinaPeople extends People {
@Override
void sayHello() {
System.out.println("会使用方法的override");
}
}
AmericanPeople.java:
package util;
public class AmericanPeople extends People{
@Override
void sayHello() {
System.out.println("会使用抽象类");
}
}
BeijingPeople.java:
package util;
public class BeijingPeople extends ChinaPeople {
@Override
void sayHello() {
System.out.println("会使用三层的继承");
}
}
main:
public static void main(String[] args) {
BeijingPeople beijingPeople = new BeijingPeople();
AmericanPeople americanPeople = new AmericanPeople();
ChinaPeople chinaPeople = new ChinaPeople();
americanPeople.sayHello();
chinaPeople.sayHello();
beijingPeople.sayHello();
}
结果:
会使用抽象类
会使用方法的override
会使用三层的继承
热心网友
时间:2024-03-09 22:24
public abstract class People {
public String name;
public String sex;
public abstract void sayHello();
}
public class ChinesePeople extends People {
@Override
public void sayHello() {
// TODO Auto-generated method stub
System.out.println("Hello, I am a Chinese...");
}
}
public class AmericanPeople extends People {
@Override
public void sayHello() {
// TODO Auto-generated method stub
System.out.println("Hello, I am an American...");
}
}
public class BeijingPeople extends ChinesePeople {
public void sayHello(){
System.out.println("Hello, I am beijing er...");
}
public static void main(String[] args) {
ChinesePeople cp = new ChinesePeople();
cp.sayHello();
AmericanPeople ap = new AmericanPeople();
ap.sayHello();
BeijingPeople bjp = new BeijingPeople();
bjp.sayHello();
}
}
输出:
Hello, I am a Chinese...
Hello, I am an American...
Hello, I am beijing er...