package demo;
public class People {
public void speak() {
System.out.println("人类具有说话的能力");
}
}
package demo;
public class Chinese extends People {
public void speak() {
System.out.println("中国人具有说汉语的能力");
}
}
package demo;
public class American extends People {
public void speak() {
System.out.println("美国人具有说英语的能力");
}
}
package demo;
public class Initial {
public static void main(String[] args) {
People sss=new Chinese();
sss.speak();
People aaa=new American();
aaa.speak();
}
}
这样可以吧
主要用来继承 多态 重写
这样可以,但建议使用abstract抽象类,更符合设计的要求
学进去了,多练练就好
父类中的方法是抽象方法
package com.imocc;
public abstract class people {
public abstract void say();
}
package com.imocc;
public class chinese extends people {
public void say(){
System.out.println("中国人说汉语");
}
}
package com.imocc;
public class american extends people {
public void say(){
System.out.println("美国人说英语");
}
}
package com.imocc;
public class inital {
public static void main(String[] args) {
people c=new chinese();
people a=new american();
c.say();
a.say();
// TODO Auto-generated method stub
}
}