package com.gezhi.interfaces;
/**
* 新建一个dog类实现接口livingable(狗吃和上厕所都是与生俱来的不应该写成接口)
* @author square 凉
*
*/
public class Dog implements Livingable{
private String dogName;
private int dogAge;
public String getDogName() {
return dogName;
}
public void setDogName(String dogName) {
this.dogName = dogName;
}
public int getDogAge() {
return dogAge;
}
public void setDogAge(int dogAge) {
this.dogAge = dogAge;
}
public void eat() {
System.out.println(dogName + " 具有吃东西的能力");
}
public void wc() {
System.out.println(dogName +" 具有上厕所的能力");
}
/**
* 新建一个dog类实现接口livingable(狗吃和上厕所都是与生俱来的不应该写成接口)
* @author square 凉
*
*/
public class Dog implements Livingable{
private String dogName;
private int dogAge;
public String getDogName() {
return dogName;
}
public void setDogName(String dogName) {
this.dogName = dogName;
}
public int getDogAge() {
return dogAge;
}
public void setDogAge(int dogAge) {
this.dogAge = dogAge;
}
public void eat() {
System.out.println(dogName + " 具有吃东西的能力");
}
public void wc() {
System.out.println(dogName +" 具有上厕所的能力");
}
}
package com.gezhi.interfaces;
/**
* 人类,也实现接口吃饭上厕所的能力(当然吃和上厕所是与生俱来的能力,不应该写成接口)
* @author square 凉
*
*/
public class People implements Livingable , ThinkAble{
/**
* 人类,也实现接口吃饭上厕所的能力(当然吃和上厕所是与生俱来的能力,不应该写成接口)
* @author square 凉
*
*/
public class People implements Livingable , ThinkAble{
private String peopleName;
private int peopleAge;
public String getPeopleName() {
return peopleName;
}
public void setPeopleName(String peopleName) {
this.peopleName = peopleName;
}
public int getPeopleAge() {
return peopleAge;
}
public void setPeopleAge(int peopleAge) {
this.peopleAge = peopleAge;
}
public void eat() {
System.out.println(peopleName + " 具有吃东西的能力");
}
public void wc() {
System.out.println(peopleName +" 具有上厕所的能力");
}
@Override
public void think() {
// TODO Auto-generated method stub
System.out.println(peopleName + " 具有思考的行为");
}
}
private int peopleAge;
public String getPeopleName() {
return peopleName;
}
public void setPeopleName(String peopleName) {
this.peopleName = peopleName;
}
public int getPeopleAge() {
return peopleAge;
}
public void setPeopleAge(int peopleAge) {
this.peopleAge = peopleAge;
}
public void eat() {
System.out.println(peopleName + " 具有吃东西的能力");
}
public void wc() {
System.out.println(peopleName +" 具有上厕所的能力");
}
@Override
public void think() {
// TODO Auto-generated method stub
System.out.println(peopleName + " 具有思考的行为");
}
}
package com.gezhi.interfaces;
/**
* 建一个服务类服务living这种能力
* @author square 凉
*
*/
public class Service {
/**
* 建一个服务类服务living这种能力
* @author square 凉
*
*/
public class Service {
/**
* 服务其他生物的行为
*/
public void serviceOther(Livingable lb) {
lb.eat();
lb.wc();
* 服务其他生物的行为
*/
public void serviceOther(Livingable lb) {
lb.eat();
lb.wc();
}
/**
* 人具有思考的行为,这个方法主要传个人(这里传了一个接口的参数,人类来实现它,主要运用了多态的特性)
* @param think
*/
public void ThinkOthers(ThinkAble think) {
/**
* 人具有思考的行为,这个方法主要传个人(这里传了一个接口的参数,人类来实现它,主要运用了多态的特性)
* @param think
*/
public void ThinkOthers(ThinkAble think) {
think.think();
}
}
}
package com.gezhi.interfaces;
/**
* 思考的能力
* @author square 凉
*
*/
public interface ThinkAble {
/**
* 思考的能力
* @author square 凉
*
*/
public interface ThinkAble {
void think();
}
}
package com.gezhi.interfaces;
/**
* 函数主入口类
* @author square 凉
*
*/
public class MainEnter {
/**
* 函数主入口类
* @author square 凉
*
*/
public class MainEnter {
public static void main(String[] args) {
// TODO Auto-generated method stub
Service se = new Service();//实例化服务类一个对象
People p = new People();//实例化人一个对象
p.setPeopleName("如花");//设置人名
se.serviceOther(p);//调用服务类的serviceOthers这个方法(本来这个参数是接口livingable的,这里小转大,传个人这个实现了接口的对象)
Dog d = new Dog();//实例化一个狗类
d.setDogName("莽子");
se.serviceOther(d);
se.ThinkOthers(p);
// TODO Auto-generated method stub
Service se = new Service();//实例化服务类一个对象
People p = new People();//实例化人一个对象
p.setPeopleName("如花");//设置人名
se.serviceOther(p);//调用服务类的serviceOthers这个方法(本来这个参数是接口livingable的,这里小转大,传个人这个实现了接口的对象)
Dog d = new Dog();//实例化一个狗类
d.setDogName("莽子");
se.serviceOther(d);
se.ThinkOthers(p);
}
}