Java小程序---接口中抽象方法的實現(解決了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();
}
}