Java小程序---接口中抽象方法的实现(解决了JAVA语言不能多继承的问题)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java小程序---接口中抽象方法的实现(解决了JAVA语言不能多继承的问题)相关的知识,希望对你有一定的参考价值。
public interface Sing {
public static final String eyecolor="black";
public void sleep();
public void sing();
}
public interface Print {
public static final String sex="woman";
public void print();
public void eat();
}
public class Student implements Sing{
String name;
public Student(String name) {
this.name=name;
}
public String getName(){
return name;
}
public void sleep(){
System.out.println("Student is sleeping");
}
public void sing(){
System.out.println("Student is singsong");
}
public void all(){
System.out.println("这位名叫"+name+"的Student是一位喜欢唱歌的人");
}
}
public class Teacher implements Sing,Print {
int age;
public Teacher(int age) {
this.age=age;
}
public int getAge(){
return age;
}
public void print() {
System.out.println("Teacher is print");
}
public void eat() {
System.out.println("Teacher is eating");
}
public void sleep() {
System.out.println("Teacher is sleeping");
}
public void sing() {
System.out.println("Teacher is singsong");
}
public void info(){
System.out.println("这位"+age+"岁的教师既会唱歌又会画画");
}
}
public class Test {
public static void main(String[] args) {
Student s1=new Student("LiQing");
s1.sleep();
s1.sing();
s1.all();
Sing t1=new Teacher(25);
t1.sing();
t1.sleep();
Print p1=(Print)t1; //(对象类型强制转换)
p1.print();
p1.eat();
Teacher t2=(Teacher)t1;
t2.info();
}
}
以上是关于Java小程序---接口中抽象方法的实现(解决了JAVA语言不能多继承的问题)的主要内容,如果未能解决你的问题,请参考以下文章