java 接口的运用
Posted 0929-luoyang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 接口的运用相关的知识,希望对你有一定的参考价值。
package 接口运用;
interface Person{
public void eat();
public void sleep();
public void play();
}
class Student implements Person{
String StudentClass;
public Student(String StudentClass) {
this.StudentClass=StudentClass;
}
public void eat() {
System.out.println(StudentClass+"在食堂吃饭!");
}
public void sleep() {
System.out.println(StudentClass+"在寝室睡觉!");
}
public void play() {
System.out.println(StudentClass+"在操场上玩!");
}
}
class Teacher implements Person{
String TeacherClass;
public Teacher(String TeacherClass) {
this.TeacherClass=TeacherClass;
}
public void eat() {
System.out.println(TeacherClass+"在教师食堂吃饭!");
}
public void sleep() {
System.out.println(TeacherClass+"住在双创楼!");
}
public void play() {
System.out.println(TeacherClass+"在双创楼打麻将");
}
}
class Parents implements Person{
String ParentsClass;
public Parents(String ParentsClass) {
this. ParentsClass= ParentsClass;
}
public void eat() {
System.out.println( ParentsClass+"在城市印象二楼吃饭");
}
public void sleep() {
System.out.println( ParentsClass+"在城市印象住着");
}
public void play() {
System.out.println( ParentsClass+"在城市印象打麻将!");
}
}
public class Dome3 {
public static void main(String[]args) {
Person s = new Student("毛塘铺大学生");
System.out.println("大家好,我在毛塘铺读大学,也就是养老!");
s.eat();
s.sleep();
s.play();
Person t = new Teacher("毛塘铺老师");
System.out.println("大家好,我在毛塘铺教书!");
t.eat();
t.sleep();
t.play();
Person p = new Parents("毛塘铺的取款机");
System.out.println("大家好,我的子女在毛塘铺读书,所以我叫毛塘铺的取款机!");
p.eat();
p.sleep();
p.play();
}
}
以上是关于java 接口的运用的主要内容,如果未能解决你的问题,请参考以下文章