java 07 接口

1.interface 

     接口

2.接口中的成员修饰符固定

    public static fianl    //成员变量    只通过类访问

    public abstract       //成员函数     

3.接口里所有的方法都是抽象的,抽象类中,可以没有抽象方法。

   类与类继承,类与接口是实现的关系。降低耦合(高内聚,低耦合)。

  通过接口实现多重继承。

4.最低标准

一、继承中的方法和字段谁可以被覆盖,为什么?

方法可以覆盖,字段不可以,因为字段是数据。

二、什么是多态

对象的多种状态,类与类继承,类与接口是实现的关系

 三、接口往类转就要强转,类往接口转不需要

class Interfacedemo{
public static void main(String[] agrs){
PC pc =new PC();
Mouse m = new Mouse();
//启动接口,调用play
pc.insertUSB(m);
}
}


//实现接口。定义以接口为参数的方法
class PC {
public void insertUSB( USB usb){
System.out.println("插入了usb");
//调用play
usb.play();
}

}

//定义接口
interface USB{
void play ();
}

//类实现接口
class Mouse implements USB{
public void play(){
System.out.println("鼠标滑动");

}
}

//三个设备插入

class Interfacedemo{
public static void main(String[] agrs){
PC pc =new PC();
//启动接口,调用play
pc.insertUSB(new Mouse(),"cdewm");
pc.insertUSB(new Mp3(),"测车位");
pc.insertUSB(new Camera(),"出卖我也");
}
}


//实现接口。定义以接口为参数的方法
class PC {
public void insertUSB( USB usb,String pg){
//System.out.println("插入了设备");
//调用接口中的play
usb.play(pg);
}

}

//定义接口
interface USB{

//抽象方法
void play (String pg );
}

//类实现接口
class Mouse implements USB{
public void play(String pg){
System.out.println(pg +"鼠标滑动");

}
}

class Mp3 implements USB{
public void play(String pg){
System.out.println(pg+"MPs 播放");

}
}

class Camera implements USB{
public void play(String pg){
System.out.println(pg+"准备照相");

}
}

//习题

class IMothernterfacedemo{
public static void main(String[] agrs){

MotherBoard M =new MotherBoard();
Vido v =new Vido();
Sound s =new Sound();
Netcard N =new Netcard();
v.play();
s.play();
N.play();
}
}

class MotherBoard {
public void insertmother(PCI p){
p.play();
}

}


//定义接口
interface PCI{
void play();
}

//实现接口
class Vido implements PCI{
public void play (){
System.out.println("插入主板声卡");
}

}

class Sound implements PCI{
public void play (){
System.out.println("插入主板音ka");
}

}

class Netcard implements PCI{
public void play (){
System.out.println("插入主板音网卡");
}

}

//多个接口的实现待修改

class Interfacedemoplus{
public static void main(String[] agrs){
Tuhao sisi =new Tuhao();
Womanstar Won =new Womanstar();
//启动接口,调用play
sisi.marry(Won);
}
}


//实现接口。定义以接口为参数的方法
class Tuhao {
public void marry(WRB w){
w.vertWhit();
w.hasmoney();
w.Beau();
System.out.println("符合条件");
}

}

//定义接口
interface Whit{
void vertWhit ();
}

interface Rich{
void hasmoney ();
}

interface Beautiful{
void Beau ();
}
//一个接口继承三个接口
interface WRB extends Whit,Rich,Beautiful {

}

//类实现接口多个接口
class Womanstar implements Whit,Rich,Beautiful {
public void vertWhit(){
System.out.println("很白~~");
}
public void hasmoney(){
System.out.println("有钱~~");
}
public void Beau(){
System.out.println("美丽~~");
}
}

//接口常量

class Interfacedemochangliang{
public static void main(String[] agrs){
Jing8 jj =new Jing8();
jj.meng();
//类名访问变量
System.out.println(Pet.leg);
}
}
//接口中加入常量
interface Pet{
int leg = 4;
void meng();

}

class Jing8 implements Pet{
public void meng(){
System.out.println(leg+"萌萌哒");
}

}

原文地址:https://www.cnblogs.com/simly/p/10234530.html