基础要点

 

 PS:静态与对象没关系

静态代码块在对象之前产生;若要初始化静态成员,则静态成员必须在静态代码块之前定义

 

 总结:

直接分号结尾,没有花括号

 接口:

接口特点:

package workhome.day04;

public class interfaceDemo {
    public static void main(String[] args) {
        WomenStar ws = new WomenStar();
        ws.white();
        ws.rich();
        
        TuHao tuhao = new TuHao();
        tuhao.marry(ws);
        
    }
}

//定义接口
interface White{
    public abstract void white();
}

interface Rich{
    public abstract void rich();
}

interface Beauty{
    public void beauty();
}


interface WRB extends White,Rich,Beauty{
    
}

 //类实现接口
class WomenStar implements WRB{
    public void white() {
        System.out.println("很白");
    }
    public void rich() {
        System.out.println("有钱");
        
    }
    @Override
    public void beauty() {
        // TODO Auto-generated method stub
        System.out.println("美");
    }
}


class TuHao{
    public void marry(WRB w) {
        w.white();
        w.beauty();
        w.rich();
        
    }
}
View Code
package workhome.day04;

public class InterfaceDemo1 {
    public static void main(String[] args) {
        Jing8  jing8 = new Jing8();
        jing8.cry();
        
        Enter e = new Enter();
        e.eat(jing8);
        
    }
}

abstract class Animal1{
    abstract void cry();
}


class Dog1 extends Animal implements Eatable{
    void cry() {
        System.out.println("汪汪");
    }
    public void eat() {
        System.out.println("吃");
    }
}

class Jing8 extends Dog1 implements Pet{
    public void cry() {
        System.err.println("jing6wuwu");
    }
    @Override
    public void buy() {
        // TODO Auto-generated method stub
        System.out.println("买");
    }
    
    
}

interface Eatable{
    public void eat();
}

interface Pet{
    public void buy();
}

class Enter{
    public void eat(Eatable m) {
        System.out.println("食物就绪了");
        m.eat();
    }
}

class RichMan{
    public void buy(Pet m) {
        m.buy();
    }
}
View Code

package workhome.day04;

public class PolyDemo1 {
    public static void main(String[] args) {
        Dog d1 = new Dog();
        d1.cry();
        d1.watch();
        
        Animal a1 = d1;
        a1.cry();
        
        Animal a2 = new Dog();
        Dog d2 = (Dog)a2;
        
        
//        Animal d2 = new Dog();
//        d2.cry();
//        
//        Object o1 = new Dog();
//        o1.cry();
    }
}


abstract class Animal{
    abstract void cry();
}

class Dog extends Animal{
    public void cry() {
        System.out.println("www");
    }
    
    public void watch() {
        System.out.println("看家");
    }
}


class Cat extends Animal{

    @Override
    void cry() {
        // TODO Auto-generated method stub
        System.out.println("miaomiao");
    }
    
}
View Code

 

 

 

 

 

原文地址:https://www.cnblogs.com/King-boy/p/10887520.html