• Java学习记录(补充四:面向对象的多态和接口)


    面对对象的多态


    //
    多态 //如何实现多态: //1:使用集成 //2:子类重写父类的方法 //3;父类引用子类对象 package day8; public abstract class Instrument { public abstract void play(); }
    package day8;
    
    public class Piano extends Instrument{
    
        @Override
        public void play() {
            System.out.println("月光曲!");
        }
        
    }
    package day8;
    
    public class Violin extends Instrument{
    
        @Override
        public void play() {
            System.out.println("葫芦娃!");
        }
    
    }
    package day8;
    
    public class Artist {
        //Instrument instrument = new piano();(只用来理解)
        public void make(Instrument instrument) {
            instrument.play();
        }
    }
    package day8;
    
    public class Test {
        public static void main(String[] args) {
            Artist user1 = new Artist();
            //实例化对象=是赋值,后面是实例化一个对象,前面是用来接收的
            Piano piano = new Piano();
            Violin violin = new Violin();
            user1.make(piano);
            user1.make(violin);
        }
    }

    结果图:

    练习:租用车(轿车和客车)
    package day8_2;
    public abstract class MotoVechicle { private String no; private String brand; private String color; private String mileage; public String getNo() { return no; } public void setNo(String no) { this.no = no; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getMileage() { return mileage; } public void setMileage(String mileage) { this.mileage = mileage; } public MotoVechicle() { super(); } public MotoVechicle(String no, String brand, String color, String mileage) { super(); this.no = no; this.brand = brand; this.color = color; this.mileage = mileage; } public abstract int calcRent(int day); }
    package day8_2;
    
    public class Car extends MotoVechicle{
        private String type;
        
        public String getType() {
            return type;
        }
    
        public void setType(String type) {
            this.type = type;
        }
        
        public Car(String type) {
            super();
            this.type = type;
        }
        public Car(String type,String no, String brand, String color, String mileage) {
            super(no,brand,color,mileage);
            this.type = type;
        }
    
        @Override
        public int calcRent(int day) {
            int price = 0;
            if (type.equals("GL8")) {
                price = 600 * day;
            }else if(type.equals("BMW")){
                price = 500 * day;
            }else if (type.equals("Buick")) {
                price = 300 * day;
            } 
            return price;
        }
        
    
    }
    package day8_2;
    
    public class Bus extends MotoVechicle{
        private int seatCount;
        
        public int getSeatCount() {
            return seatCount;
        }
        public void setSeatCount(int seatCount) {
            this.seatCount = seatCount;
        }
        
        public Bus(int seatCount) {
            super();
            this.seatCount = seatCount;
        }
        public Bus(int seatCount,String no, String brand, String color, String mileage) {
            super(no,brand,color,mileage);
            this.seatCount = seatCount;
        }
        @Override
        public int calcRent(int day) {
            int price = 0;
            if (seatCount > 16) {
                price = 1500 * day;
            }else if (seatCount <= 16) {
                price = 800 * day;
            }
            return price;
        }
    
    }
    package day8_2;
    
    public class CalcTest {
        public int calc(MotoVechicle motoVehicle,int day){
            int price = motoVehicle.calcRent(day);
            return price;
        }
        public static void main(String[] args) {
            int result = 0;
            Car car = new Car("GL8");
            CalcTest calcPrice = new CalcTest();
            result = calcPrice.calc(car, 7);
            System.out.println("租两天GL8的价格为:"+result);
        }
    }

    结果图:

    面对对象接口

    //
    接口:接口是一种规范 //接口不可以被实例化 //接口里面的方法都是抽象的 package day8_3; public interface InterfaceDemo { //1:接口不能被实例化 //2:接口里面的方法默认都是public类型的抽象方法 //所有public abstract修饰可以忽略 //3:类是单继承 但是类可以实现多个接口 //4:在接口里定义的属性都是public类型的静态常量 // public static final可以省略 //5:接口可以继承多个接口,可以得到父级接口里面的"所有"的属性和方法 public static final int AGE = 10; void showName(); }
    package day8_3;
    
    public class UDisk implements UsbInterface {
    
        public void service() {
            System.out.println("连接USB接口,开始传输数据");
        }
        public static void main(String[] args) {
            //多态的一种,向上转型(多态就是"父类引用子类对象!!!")
            UsbInterface uDisk = new UDisk();
            uDisk.service();
        }
    }
    package day8_3;
    
    public interface UsbInterface {
        void service();
    }

    结果图:

  • 相关阅读:
    Maximal Square
    Count Complete Tree Nodes
    Rectangle Area
    Implement Stack using Queues
    Basic Calculator
    Invert Binary Tree
    Summary Ranges
    Basic Calculator II
    Majority Element II
    Kth Smallest Element in a BST
  • 原文地址:https://www.cnblogs.com/lizuowei/p/7425257.html
Copyright © 2020-2023  润新知