-------android培训、java培训、期待与您交流! ----------
1.class 用于定义类
2.interface 用于定义接口
3.定义格式如下:
常量:public static final 大写
方法:public abstract
4.接口中的成员都是 public
5.接口不可以创建对象,需要被子类实行
6.接口可以被类多实现
interface Inter
{
public static final int NUM = 5;
public abstract void show();
}
class InterfaceDemo
{
pulbic static void main(String args[])
{
System.out.println();
Test t = new Test();
System.out.println(t.NUM);
System.out.println(Test.NUM);
}
}
class Test implements inter,InerA
{
public void show()
{
System.out.println();
}
public abstract void max()
{
System.out.println();
}
}
interface InerA
{
public abstract void max();
}
引用类型: 接口--------------类-------------------数组
<----------------多态-------------------->
abstract class Animal
{
public abstract void eat();
}
class Cat extends Animal()
{
public void eat()
{
System.out.println("吃鱼");
}
public void catcth()
{
System.out.println("抓老鼠");
}
}
class Dog extends Animal
{
public void eat()
{
System.out.println("骨头");
}
}
public class Demo
{
public static void main()
{
Cat c = new Cat();
Dog d = new Dog();
c.eat();
d.eat();
}
public static void function(Cat c )
{
c.eat();
} //作用封装方法<*********************************>
public static void function(Dog d )
{
d.eat();
} //作用封装方法
}
<-----------------多态 转型-------------------->
class DuoTaiDemo
{
public static void main(String args[])
{
Animal a = new Cat (); //类型提升,向上转型
a.eat();
// 想用子类的方法,将父类的引用转换成子类
Animal a = new Cat ();
Cat c = (Cat) a ;
c. catcth();
}
}
function(new Cat());
function(new Dog());
public static void function(Animal a)
{
if(a instanceof Cat)
{
}
if(a instanceof Dog)
{
}
}
面向对象多态实例
interface PCI
{
public void open();
public void close();
}
class MainBoard
{
public void run()
{
System.out.println(" run ");
}
public void userPCI(PCI P) //PCI p = new NetCard()
{
if(p!=null)
{
p.open();
p.close();
}
}
}
class NetCard implements PCI
{
public void open()
{
System.out.println(" open ");
}
public void close ()
{
System.out.println(" close ");
}
}
class Demo
{
public static void main(String args[])
{
MainBoard mb = new MainBoard();
mb.run();
mb.userPCI(null);
mb.userPCI(new NetCard() );
}
}
-------android培训、java培训、期待与您交流! ----------
详细请查看:http://edu.youkuaiyun.com/heima/