JAVA自学笔记(5)

JAVA的知识储备升级

Day1 面向对象之多态

1.0 多态的格式

JAVA自学笔记(5)_第1张图片

 

2.0多态中成员变量的使用特点

public class FU {
   int num=10;
   public void meth()
   {
       System.out.println(num);
   }
}
public class ZI extends FU{
       int num=20;
       @Override
       public void meth()
       {
           System.out.println(num);
       }
} 
public class Main {
    public static void main(String[] args) {
        FU fu=new ZI();
        System.out.println(fu.num); //10;
        fu.meth();//20
    }
}

3.0 多态中成员方法的使用特点

JAVA自学笔记(5)_第2张图片

public class FU {
   public void meth1()
   {
       System.out.println("AAA");
   }
   public void methfu()
   {
       System.out.println("父类特有方法!");
   }
}

  

public class ZI extends FU{
      public void  methzi()
      {
          System.out.println("子类特有方法!" );
      }
}

  

public class Main {
    public static void main(String[] args) {
        FU fu=new ZI();
        //fu.methzi()  编译看左边 父类没有methzi()
        fu.methfu();// 执行看右边 没有则向上找
        fu.meth1();
     }
}

4.0 对象的向上和向下转型

JAVA自学笔记(5)_第3张图片

public abstract class Animals {
  public abstract void eat();
}

 

public class Cat extends Animals {

    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }
    public void catchmouse()
    {
        System.out.println("猫捉老鼠");
    }
}

  

public class Main {
    public static void main(String[] args) {
       Animals animal=new Cat();
       animal.eat();
       //animal.catchmouse();//编译看左边 父类无此方法
        //向下转型
        Cat cat=(Cat)animal;
        cat.catchmouse();

     }
}

打印结果:

猫吃鱼
猫捉老鼠

5.0 笔记本USB接口案例 实现

JAVA自学笔记(5)_第4张图片

 

public interface USB {
    public abstract void opendevice();
    public abstract void offdevice();
}

  

public class Computer {
    public void open()
    {
        System.out.println("电脑开机");
    }
    public void off()
    {
        System.out.println("电脑关机");
    }
    public void opendevices(USB usb)
    {
        usb.opendevice();
        if(usb instanceof Keyboard)
        {
            Keyboard keyboard=(Keyboard)usb;
            keyboard.type();
        }
        else if(usb instanceof Mouse)
        {
            Mouse mouse=(Mouse) usb;
            ((Mouse) usb).click();
        }
        usb.offdevice();
    }

}

  

public class Keyboard implements USB{

    @Override
    public void opendevice() {
        System.out.println("打开键盘");
    }

    @Override
    public void offdevice() {
        System.out.println("关闭键盘");
    }
    public void type()
    {
        System.out.println("键盘输入");
    }
}

  

public class Mouse implements USB{
    @Override
    public void opendevice() {
        System.out.println("打开鼠标");
    }

    @Override
    public void offdevice() {
        System.out.println("关闭鼠标");
    }
    public void click()
    {
        System.out.println("鼠标点击");
    }
}

  

public class Main {
    public static void main(String[] args) {
     Computer com=new Computer();
     com.open();
     //准备一个鼠标,供电脑使用
     //Mouse mouse=new Mouse();
     //USB usb=mouse;//向上转型
     USB usb=new Mouse();//多态做法
     com.opendevices(usb);
     Keyboard keyboard=new Keyboard();//没使用向上转型 这里可以理解为自动转型 由小范围到大范围
     com.opendevices(keyboard);
     //com.opendevices(new Keyboard())
     com.off();
     }
}

打印结果:

电脑开机
打开鼠标
鼠标点击
关闭鼠标
打开键盘
键盘输入
关闭键盘
电脑关机

Day2 final关键字

1.0 final关键字概念与四种用法

JAVA自学笔记(5)_第5张图片

2.0 final修饰一个类

JAVA自学笔记(5)_第6张图片

3.0 final修饰一个局部变量

public class People {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

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

}

  

public class FinalMain {
    public static void main(String[] args) {
        int num1=10;
        System.out.println(num1);
        num1=20;
        System.out.println(num1);
        final int num2=30;
        //num2=40
        //num2=30
        System.out.println(num2);
        final int num3;
        num3=40 ;//正确方法!
        System.out.println(num3);
        People people1=new People("金泰亨");
        System.out.println(people1);//地址
        people1=new People("田柾国");
        System.out.println(people1);

        people1.setName("朴智旻");//内容可以改变 对引用类型来说不可改变的是地址

    }
}

 打印结果:

10
20
30
40
People@50cbc42f
People@75412c2f

4.0 final修饰成员变量

JAVA自学笔记(5)_第7张图片

public class Finaldemo {
    private final String name;
    //通过构造方法赋值
    public Finaldemo() {
        name="徐穗珍";
    }

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

    public String getName() {
        return name;
    }

}

Day3 内部类

1.0 内部类的概念、分类以及定义

JAVA自学笔记(5)_第8张图片

 

 

2.0 内部类的使用

JAVA自学笔记(5)_第9张图片

 

 

 

public class Outer {//间接方式
    class Inner{
        public void methodinner()
        {
            System.out.println("内部方法执行!");
        }
    }
    public void methodouter()
    {
        System.out.println("外部方法执行!");
        new Inner().methodinner();
    }
}
public class Main {
    public static void main(String[] args) {
        Outer outer=new Outer();
        outer.methodouter();
    }
}

直接方式

public class Outer {
    class Inner{
        public void methodinner()
        {
            System.out.println("内部方法执行!");
        }
    }
    public void methodouter()
    {
        System.out.println("外部方法执行!");
    }
}

  

public class Main {
    public static void main(String[] args) {
        Outer.Inner inner=new Outer().new Inner();
        inner.methodinner();
    }
}

 

打印结果:

内部方法执行!

3.0 内部类的同名变量的访问

 

public class Outer {
    int num=10;
    class Inner {
        int num=20;
        public void method()
        {
            int num=30;
            System.out.println(num);//30
            System.out.println(this.num);//20
            System.out.println(Outer.this.num);//10
        }
    }
}

 

4.0 局部内部类的定义

JAVA自学笔记(5)_第10张图片 

 

public class Outer {
   public void methodOuter()
   {
      class Inner
      {
          int num=10;
          public void metehod()
          {
              System.out.println(num);
          }
      }
      Inner inner=new Inner();
      inner.metehod();
   }


}

  

public class Main {
    public static void main(String[] args) {
    Outer outer=new Outer();
    outer.methodOuter();
    }
}

 

打印结果:

10

5.0 局部内部类的final问题

JAVA自学笔记(5)_第11张图片

6.0 匿名内部类

1)使用

JAVA自学笔记(5)_第12张图片 

public interface Interface {
    public abstract void method();
}

  

public class Main {
    public static void main(String[] args) {
    Interface impl=new Interface() {
        @Override
        public void method() {
            System.out.println("接口覆盖重写方法执行");
        }
    };
    impl.method();
    }
}

 

打印结果:

接口覆盖重写方法执行

2)注意事项

JAVA自学笔记(5)_第13张图片

 

7.0 类作为成员变量

public class Weapon {
   private String device;

    public Weapon() {
    }

    public Weapon(String device) {
        this.device = device;
    }

    public String getDevice() {
        return device;
    }

    public void setDevice(String device) {
        this.device = device;
    }
}

  

public class Hero {
    private String name;
    private int age;
    private Weapon weapon;

    public Hero() {
    }

    public Hero(String name, int age) {
        this.name = name;
        this.age = age;
    }

    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 Weapon getWeapon() {
        return weapon;
    }

    public void setWeapon(Weapon weapon) {
        this.weapon = weapon;
    }
    public void crack()
    {
        System.out.println(getAge()+" "+getName()+" 使出了"+weapon.getDevice());
    }

}

  

public class Main {
    public static void main(String[] args) {
    Hero hero=new Hero("妲己",18);
    Weapon weapon=new Weapon();
    weapon.setDevice("放大招");
    hero.setWeapon(weapon);
    hero.crack();
    }
}

 

打印结果:

18 妲己 使出了放大招

 

8.0 使用接口作为成员变量

public interface Skill {
    public abstract void device();
}

  

public class Interfaced implements Skill{

    @Override
    public void device() {
        System.out.println("背水一战");
    }
}

  

public class Hero {
    private int age;
    private String name;
    private Skill skill;

    public Hero() {
    }

    public Hero(int age, String name, Skill skill) {
        this.age = age;
        this.name = name;
        this.skill = skill;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Skill getSkill() {
        return skill;
    }

    public void setSkill(Skill skill) {
        this.skill = skill;
    }
    public void crack()
    {
        System.out.print("我是"+getAge()+"岁的"+getName()+"正在放出技能");
        skill.device();
    }
}

  

public class Main {
    public static void main(String[] args) {

        Interfaced impl=new Interfaced();
        Hero hero=new Hero();
        hero.setName("韩信");
        hero.setAge(18);
        /*
        *第一种创建Skill接口的实现类
        * hero.setSkill(impl);
        * hero.crack();
        * 我是18岁的韩信正在放出技能背水一战
        * */

        /*
        * 第二种使用匿名内部类
        * Skill skill=new Skill()  {
            @Override
            public void device() {
                System.out.println("背水一战");
            }
        };
        * hero.setSkill(skill);
        * hero.crack();//我是18岁的韩信正在放出技能背水一战
        * */

        /*第三种使用匿名对象 匿名内部类*/
        hero.setSkill(new Skill() {
            @Override
            public void device() {
                System.out.println("背水一战");
            }
        });
        hero.crack();//我是18岁的韩信正在放出技能背水一战
    }
}

  

你可能感兴趣的:(JAVA自学笔记(5))