Java(面向对象进阶——接口)
接口的定义和使用
练习
package day10.接口练习;
public abstract class animals {
private String name;
private int age;
public animals(){
}
public animals(String name,int age){
this.name=name;
this.age=age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public abstract void eat(String something);
}
package day10.接口练习;
public class dog extends animals implements swing{
public dog(String name,int age){
super(name,age);
}
@Override
public void eat(String something) {
System.out.print(getAge()+"岁的"+"小狗吃"+something);
}
@Override
public void youyong(String fangfa) {
System.out.println(fangfa);
}
}
package day10.接口练习;
public class frog extends animals implements swing{
public frog(String name,int age){
super(name,age);
}
@Override
public void eat(String something) {
System.out.print(getAge()+"岁的"+"青蛙吃"+something);
}
@Override
public void youyong(String fangfa) {
System.out.print(fangfa);
}
}
package day10.接口练习;
public class tuzi extends animals{
public tuzi (String name,int age){
super(name,age);
}
public void eat(String something){
System.out.println();
System.out.println(getAge()+"岁的"+"兔子吃"+something);
}
}
package day10.接口练习;
public interface swing {
public abstract void youyong(String fangfa);
}
package day10.接口练习;
public class test {
public static void main(String[] args) {
dog d=new dog("小黄",2);
frog f=new frog("小青",6);
tuzi t=new tuzi("突牙",3);
d.eat("骨头");
d.youyong("狗刨");
f.eat("虫子");
f.youyong("蛙泳");
t.eat("胡萝卜");
}
}
接口中成员的特点
接口和类之间的关系
练习
package day10.pingpang;
public class people {
private String name;
private int age;
public people(){
}
public people (String name,int age){
this.age=age;
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package day10.pingpang;
public abstract class jiaolian extends people{
public jiaolian(String name,int age){
super(name,age);
}
public abstract void jiao(String qiu);
}
package day10.pingpang;
public abstract class xueyuan extends people{
public xueyuan(String name,int age){
super(name,age);
}
public abstract void xue(String qiu);
}
package day10.pingpang;
public class lanqiu_jiaolian extends jiaolian{
public lanqiu_jiaolian(String name, int age) {
super(name, age);
}
@Override
public void jiao(String qiu) {
System.out.println(getName()+"年龄为"+getAge()+"教打"+qiu);
}
}
package day10.pingpang;
public class pingpang_xueyuan extends people implements English{
public pingpang_xueyuan(String name,int age){
super(name,age);
}
@Override
public void speak(String langer) {
System.out.print(getName()+"年龄为"+getAge()+"会讲"+langer);
}
//@Override
public void xue(String qiu){
System.out.println("学习打"+qiu);
}
}
package day10.pingpang;
public class lanqiu_xueyuan extends xueyuan{
public lanqiu_xueyuan(String name,int age){
super(name,age);
}
//@Override
public void xue(String qiu){
System.out.println(getName()+"年龄为"+getAge()+"学习打"+qiu);
}
}
package day10.pingpang;
public class pingpang_jiaolian extends jiaolian implements English{
public pingpang_jiaolian(String name, int age) {
super(name, age);
}
@Override
public void jiao(String qiu) {
System.out.print(getName()+"教打"+qiu);
}
@Override
public void speak(String langer) {
System.out.println("会讲"+langer);
}
}
package day10.pingpang;
public class test {
public static void main(String[] args) {
pingpang_jiaolian p1=new pingpang_jiaolian("李教练",38);
pingpang_xueyuan p2=new pingpang_xueyuan("王小生",18);
p1.jiao("乒乓球");
p1.speak("英语");
p2.speak("英语");
p2.xue("乒乓球");
System.out.println("----------------");
lanqiu_jiaolian p3=new lanqiu_jiaolian("姚明",43);
lanqiu_xueyuan p4=new lanqiu_xueyuan("姚小明",16);
p3.jiao("篮球");
p4.xue("篮球");
}
}
接口中的方法
默认方法
静态方法
私有方法
适配器设计模式 (设置中间人)