08 JAVA 继承多态抽象类接口

hoshino- / 2024-10-10 / 原文

1 函数题

1,super.a 来明确访问父类的字段。super(a); 表示调用父类的构造函数,并传递参数 a
2,
抽象类继承需要写新的构造器,重写抽象方法

class Circle extends shape{
    private double radius;
    public Circle(double radius){
        this.radius=radius;

    }
    //构造器比较容易忘
    //新的类型有新的变量所以要用构造器初始化
    @Override
    public  double getArea(){
        return Math.PI*radius*radius;
    }// 求面积

    public  double getPerimeter(){
        return 2*Math.PI*radius;
    } // 求周长
}

3,比较类对象


public boolean equals(Object o) {
    if (this == o) return true; // 同一对象
    if (o == null || getClass() != o.getClass()) return false; // 空或不同类
    Point point = (Point) o; // 类型转换
    return this.xPos == point.xPos && this.yPos == point.yPos; // 属性比较
	/*不能直接写this.id=id,这个不是传入的参数,不能直接调用,需要通过类*/
}

//而不是
   public boolean equals(int x,int y)
        {
            if((this.xPos==x)&&(this.yPos==y))
                return true;
            else
                return false;
        }
		
//另一个

public boolean equals(Object o) {
    if (this == o) return true; // 同一对象
    if (o == null || getClass() != o.getClass()) return false; // 空或不同类
    Student student = (Student) o; // 类型转换
    return this.id==student.id; // 属性比较
}

4,接口

class Octagon implements Comparable<Octagon>,Cloneable{
    private double side;
    public Octagon(double side) {
        this.side = side;
    }
    public double getSide() {
        return side;
    }
    public void setSide(double side) {
        this.side = side;
    }
    public double getPerimeter()
    {
        return side*8;
    }
    public double getArea()
    {
        return (2+4/Math.sqrt(2))*side*side;
    }
    @Override
    public int compareTo(Octagon o){
        if(this.side>o.getSide())
            return 1;
        else if(this.side<o.getSide())
            return -1;
        else
            return 0;
    }
    /*
    在 Java 中,Comparable<T> 是一个泛型接口,
    用于定义对象的自然顺序。这里的 <T> 表示一个类型参数,
    允许您指定将要比较的对象的类型。
    在 Comparable<Octagon> 中,<> 符号是用于指定泛型类型参数的
    意味着在调用 compareTo 方法时,
    参数必须是 Octagon 类型,确保类型安全。
     */
    @Override
    protected Object clone() {
        return this;
    }
}

5,类和子类

class Account{
    private  int id;
    private int balance;
    public Account() {
    }
    public Account(int id, int balance) {
        this.id = id;
        this.balance = balance;
    }
    public int getBalance() {
        return balance;
    }
    public void setBalance(int balance) {
        this.balance = balance;
    }
    public boolean withdraw(int money)
    {
        int tmp=balance-money;
        if(tmp>=0)
            setBalance(tmp);
        return tmp>=0;
    }
    public void deposit(int money)
    {
        balance+=money;
    }
    public String toString()
    {
        return "The balance of account "+id+" is "+balance;
    }
 }
 class CheckingAccount extends Account{
    private int overdraft;
    public CheckingAccount() {
    }
    public CheckingAccount(int id, int balance, int overdraft) {
        super(id, balance);
        this.overdraft = overdraft;
    }
         @Override
    public boolean withdraw(int money) {
       int tmp=getBalance()+overdraft-money;
       if(tmp>=0)
           setBalance(getBalance()-money);
       return tmp>=0;
    }
 }

6,因为传入参数的不确定,我们要手动调用有参和无参方法。

 public void pay(int hour)
    {
        pay();
    }
    public void pay()
    {
        System.out.println(40.0*rate);
    }

7,继承

class Media{
    String name;
    public double getDailyRent(){
        return 0.0;
    }
}
class Book extends Media{
    double price;
    public Book(String name,double price)
    {
        this.name=name;
        this.price=price;
    }
    //构造块
    public double getDailyRent(){
        return 0.01*price;
        //重写
    }
}
class DVD extends Media{
    public DVD(String name)
    {
        this.name=name;
    }
    public double getDailyRent(){
        return 1;
    }
}
class MediaShop{
    public static double calculateRent(Media[] medias, int days){
        double sum=0;
        //静态方法全局方法,数组长度没有括号
        for(int i=0;i<medias.length;i++)
        {
            sum+=medias[i].getDailyRent()*days;
        }
        return sum;
    }
}

8,子类的继承

image
image
image

//样例2不通过

class ComputerPlayer extends Player {
    String name;

    public ComputerPlayer(String name) {
//        this.name = name;这样写需要在父类定义无参构造器
        super(name);
    }
}

class PersonPlayer extends Player {
    String name;
    public PersonPlayer(String name) {
//        this.name = name;
        super(name);
    }

    int show() {
        Scanner sc = new Scanner(System.in);
        int show = sc.nextInt();
        return show;
    }
}

class Game {
    int A;
    int B;

    Game(Player A, Player B) {
        this.A = A.show();
        this.B = B.show();
    }

    public void start() {
        if (A == B) {
            System.out.println("A Draw.");
        } else if ((A == 1 && B == 2) || (A == 2 && B == 3) || (A == 3 || B == 1)) {
            System.out.println("Winner is personPlayer.");
        } else {
            System.out.println("Winner is computerPlayer.");
        }
    }
}