码迷,mamicode.com
首页 > 编程语言 > 详细

java新手笔记21 接口

时间:2015-07-25 15:09:04      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

1.接口

package com.yfs.javase;

public interface IDemo1 {//interface  接口
	
	public /*abstract*/ void method1();//默认抽象方法 public

}

class Child1 implements IDemo1 {

	@Override
	public void method1() {  
		// 实现接口的方法
		System.out.println("call method1()");
	}
	
}

 2.接口示例1

package com.yfs.javase;

public interface IFly {
	
	public void fly();

}

 3.实现接口

package com.yfs.javase;

public class Bird implements IFly {

	@Override
	public void fly() {
		System.out.println("鸟在飞....");

	}

}
class Wuya  extends Bird {
	@Override
	public void fly() {
		System.out.println("乌鸦在飞....");

	}
}

class Butterfly implements IFly {

	@Override
	public void fly() {
		System.out.println("蝴蝶在飞....");
		
	}
	
}

 4.IRun接口

package com.yfs.javase;

public interface IRun {
	int a = 10;//默认public static final
	public void run();

}
/*public*/ interface ISwim {
	
}
class Horse implements IRun {

	@Override
	public void run() {
		
		System.out.println("马在跑..." + a);
		
	}
	
}

 5.接口多继承

package com.yfs.javase;
//接口可以多继承
public interface ITest extends IFly, IRun, ISwim {

}

 6.Person

package com.yfs.javase;

public class Person {

	private String name;
	private int age;
	private char sex;

	public Person() {
		
	}

	public Person(String name) {
		this.name = name;
	}

	public Person(String name, int age, char sex) {
		this.name = name;
		this.age = age;
		this.sex = sex;
	}

	public void introduce() {
		System.out.println("I am Person....");
	}

	public String toString() {
		return "姓名:" + name + "  年龄 :" + age + "  性别:" + sex;
	}

	public void speak() {
		System.out.println(name + " 工作了吗?");
	}

	public void sleep() {
		System.out.println(name + " 睡觉了吗?");
	}

	public void eat() {
		System.out.println(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;
	}

	public char getSex() {
		return sex;
	}

	public void setSex(char sex) {
		this.sex = sex;
	}

}

 7.超人 继承 人,实现 IRun,IFly 接口

package com.yfs.javase;

public class SuperMan extends Person implements IRun, IFly {

	@Override
	public void run() {
		System.out.println("超人在跑....");
		
	}

	@Override
	public void fly() {
		System.out.println("超人在飞...");
		
	}

}

 8.接口 测试

package com.yfs.javase;

public class Test1 {

	
	public static void main(String[] args) {
		IFly f1 = null;
		//		new IFly();//接口不能实例化
		f1 = new Bird();
		f1.fly();
		f1 = new Butterfly();
		f1.fly();
		
		callMethod(new Wuya());
		
		IRun r1 = new Horse();
		r1.run();

		
		callMethod(new SuperMan());
	}
	
	public static  void callMethod(IFly f) {
		f.fly();
	}

}

 9.测试2

package com.yfs.javase;

public class Test2 {

	public static void main(String[] args) {
		SuperMan s1 = new SuperMan();
		s1.setName("Tom");
		s1.speak();
        s1.run();
        
        callMethod(new Horse());
        callMethod(s1);
	}

	 
	public static void callMethod(IRun r) {
		r.run();
	}
}

 

java新手笔记21 接口

标签:

原文地址:http://www.cnblogs.com/feilongblog/p/4675867.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!