接口的小例子



package lin.sxt;


class Student implements Persion
{
 private String name;
 private String password;
 private String school;
 public Student(){}
 public Student(String name,String password,String school)
 {
  this.name = name;
  this.password = password;
  this.school = school;
 }
 
 /**
  * @return Returns the school.
  */
 public String getSchool() {
  return school;
 }
 /**
  * @param school The school to set.
  */
 public void setSchool(String school) {
  this.school = school;
 }
 /**
  * @return Returns the name.
  */
 public String getName() {
  return name;
 }
 /**
  * @param name The name to set.
  */
 public void setName(String name) {
  this.name = name;
 }
 /**
  * @return Returns the password.
  */
 public String getPassword() {
  return password;
 }
 /**
  * @param password The password to set.
  */
 public void setPassword(String password) {
  this.password = password;
 }
}
class Teacher implements Persion
{
 private String name;
 private String password;
 private String school;
 private double wags;
 public Teacher(){}
 public Teacher(String name,String password,String school,double wags)
 {
  this.name = name;
  this.password = password;
  this.school = school;
  this.wags = wags;
 }
 /**
  * @return Returns the name.
  */
 public String getName() {
  return name;
 }
 /**
  * @param name The name to set.
  */
 public void setName(String name) {
  this.name = name;
 }
 /**
  * @return Returns the password.
  */
 public String getPassword() {
  return password;
 }
 /**
  * @param password The password to set.
  */
 public void setPassword(String password) {
  this.password = password;
 }
 /**
  * @return Returns the school.
  */
 public String getSchool() {
  return school;
 }
 /**
  * @param school The school to set.
  */
 public void setSchool(String school) {
  this.school = school;
 }
 /**
  * @return Returns the wags.
  */
 public double getWags() {
  return wags;
 }
 /**
  * @param wags The wags to set.
  */
 public void setWags(double wags) {
  this.wags = wags;
 }
}
class School
{
 private String name;
 private Persion p;
 public School(){}
 public School(String name,Persion p)
 {
  this.name = name;
  this.p = p;
 }
 
 /**
  * @return Returns the name.
  */
 public String getName() {
  return name;
 }
 /**
  * @param name The name to set.
  */
 public void setName(String name) {
  this.name = name;
 }
 public void show()
 {
  System.out.println(p.getName());
  System.out.println(p.getPassword());
  System.out.println(p.getClass());
 }
}
public class InterfaceTest {
 

 public static void main(String[] args) {
  Persion a = new Student("zhangsan","0000","beijindaxue");
  Persion b = new Teacher("lisi","5555","qinghuadaxue",5000.0);
  School s1 = new School("mz",a);
  School s2 = new School("nt",b);
  s1.show();
  s2.show();
  System.out.println("------------------");
  Student a1 = (Student)a;
  System.out.println(a1.getSchool());
  
  
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值