学习java接口知识

学习java接口知识

//一个类最多只能有一个直接的父类。但是有多个间接的父类。  java是单继承。

class ye_01{    
    String name;
}
class fu_01 extends ye_01{}
class zi_01 extends fu_01{}

public class Interface {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        zi_01 zi= new zi_01();
        zi.name="bebe";
        System.out.println(zi.name);
    }
}

以上问题和java单继承相违背,那么就需要用接口来解决。

接口:拓展功能的。
接口的定义格式:

interface 接口名{
    }

实现接口的格式:

class  类名 implements 接口名{
    }

如下代码:

interface A { // 接口A,
    // 成员变量
    public static final int i = 10;
    // 成员函数
    public void print();
}

public class Interface_02 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Interface_02 interface_02 = new Interface_02();
        interface_02.print();
    }
    // 实现接口中的方法
    public void print() {
        System.out.println("这个是接口中的print方法...");
    }
}

接口要注意的事项 :
    1. 接口是一个特殊的类。
    2. 接口的成员变量默认的修饰符为: public static final 。那么也就是说接口中的成员变量都是常量。
    3. 接口中 的方法都是抽象的方法,默认的修饰符为: public abstract。
    4. 接口不能创建对象,使用implements引用。
    5. 接口是没有构造方法的,因为接口中的变量已经初始化了,没有必要有构造方法。
    6. 接口是给类去实现使用的,非抽象类实现一个接口的时候,必须要把接口中所有方法全部实现。

然后我们看看怎么解决带橡皮的铅笔的问题:

//普通的铅笔类
class Pencil{
    String name;
    public Pencil(String name){
        this.name = name;
    }
    public void write(){
        System.out.println(name+"沙沙的写...");
    }
}

//橡皮接口
interface Eraser{
    public void remove();
}

//带橡皮的铅笔
class PencilWithEraser extends Pencil implements Eraser {
    public PencilWithEraser(String name){
        super(name);
    }
    public void remove(){
        System.out.println("涂改,涂改....");
    }
}

class Demo8 
{
    public static void main(String[] args) 
    {
        //System.out.println("Hello World!");
        PencilWithEraser p = new PencilWithEraser("2B铅笔");
        p.write();
        p.remove();
    }
}

接口的作用:
    1. 程序的解耦。    (低耦合)--接口最重要的优势
    2. 定义约束规范。(项目组长和项目组长的责任)--次重要
    3. 拓展功能。--最基本的


例如开发一个网站:

每个模块都会有增、删、查、改;

每个项目都需要后期的维护(不断优化代码、优化功能);【社会招聘-2天的适应期就开始干活了】

看网站代码的技巧:只看一个模块即可,因为大项目一般大家用的都是共同模块。

/*
 * 需求:在现实生活中有部分同学在学校期间只会学习,但是有部分学生除了学习外还会赚钱。
 *  interface (定义接口)
 *  implements (实现接口)
 * 
 * */

//普通的学生类
class Student{
    String name;
    public Student(String name){
        this.name = name;
    }
    public void study(yunshenggw.cn/){
        System.out.println(name+"好好学习");
    }
}

//挣钱是学生拓展的功能---定义在接口上
interface Money{
    public void makeMoney();
}

//会挣钱的学生
class MoneyStudent extends Student implements Money{
    public MoneyStudent(String name){
        super(name);
    }
    public void makeMoney(){
        System.out.println(name+"好好挣钱,然后交学费!");
    }
}

public class Interface_03 {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Student s = new Student("李金华");
        s.study();
        //s.makeMoney();
        
        MoneyStudent m = new MoneyStudent("孙双双");
        m.study();
        m.makeMoney();
    }
}

类与接口之间的关系:实现的关系

package test_01;
/*
类与接口之间关系: 实现关系。

类与接口要注意的事项:
    1. 非抽象类实现一个接口时,必须要把接口中所有方法全部实现。
    2. 抽象类实现一个接口时,可以实现也可以不实现接口中的 方法。
    3. 一个类可以实现多个接口 。
    疑问: java为什么不支持多继承,而支持了多实现呢?

    class A{
             public void print(www.tianjiuyule178.com ){
             System.out.println("AAAAAA");
             }
           }

    class B{
             public void print(baihuiyulegw.com){
             System.out.println("BBBBBB");
             }
           }
    class C extends A ,B{
         
            }
   
    new C().print();   //调用之后就会很尴尬,因为有冲突


    答案: 接口是调用自己实现的那一个就可以,不存在冲突。而继承是使用父类的方法,可能会发生冲突;
    
    接口与接口之间关系: 继承关系。    
    接口与接口之间要注意事项:一个接口是可以继承多个接口的。
    
*/
interface Ab{    
    public void print();
}

interface Ac extends Ab{     //Ac继承了Ab
    public void getArea(www.yunsengyule.com);
}

//abstract class ANimal implements Ab,Ac{
//    public void print(){
//        System.out.println(" 实现 Ab 接口");
//    }
//    public void getArea(){
//        System.out.println(" 实现 Ac 接口");
//    }
//    
//}

public class Interface_04 implements Ac{

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Interface_04 interface_04= new Interface_04();    
        interface_04.getArea();
        interface_04.print();

    }
    public void print(www.gouyiflb.cn){
        System.out.println(" 实现 Ab 接口");
    }
    public void getArea(www.yscylept.com){
        System.out.println(" 实现 Ac 接口");
    }

猜你喜欢

转载自www.cnblogs.com/qwangxiao/p/10778069.html