抬头仰望星空,是否能发现自己的渺小。

伪斜杠青年

人们总是混淆了欲望和理想

JAVA实验7.2:附加

题目1:

package Compare;

public abstract class CompareObject {
    public abstract int CompareTo(Object obj);
}
package Compare;

public class Position extends CompareObject {
    private int x;
    private int y;

    public Position() {
    }

    public Position(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int CompareTo(Object obj) {
        Position pos = (Position) obj;
        double dist1 = Math.sqrt(x * x + y * y);
        double dist2 = Math.sqrt(pos.x * pos.x + pos.y * pos.y);
        return (int) (dist1 - dist2);
    }

    public static void main(String[] args) {
        Position pos1 = new Position(0, 0);
        Position pos2 = new Position(3, 4);
        System.out.println(pos1.CompareTo(pos2));
    }
}

题目2:

package Flyable;

public interface Flyable {
    public abstract void takeoff();

    public abstract void land();

    public abstract void fly();
}
package Flyable;

public abstract class Vehicle {
}
package Flyable;

public abstract class Animal {
    public abstract void eat();
}
package Flyable;

public class Bird extends Animal implements Flyable {
    public void takeoff() {
        System.out.println("Bird takeoff.");
    }

    public void land() {
        System.out.println("Bird land.");
    }

    public void fly() {
        System.out.println("Bird flying.");
    }

    public void eat() {
        System.out.println("Bird eating.");
    }

    public void buildNest() {
        System.out.println("Bird building nest.");
    }

    public void layEggs() {
        System.out.println("Bird lying eggs.");
    }
}
package Flyable;

public class Superman extends Animal implements Flyable {
    public void takeoff() {
        System.out.println("Superman takeoff.");
    }

    public void land() {
        System.out.println("Superman land.");
    }

    public void fly() {
        System.out.println("Superman flying.");
    }

    public void eat() {
        System.out.println("Superman eating.");
    }

    public void leapBuilding() {
        System.out.println("Superman leap Building.");
    }

    public void stopBullet() {
        System.out.println("Superman stop Bullet.");
    }
}
package Flyable;

public class PolymorphismDemo {

    public static void main(String[] args) {
        Flyable plane = new AirPlane() {
        };
        plane.takeoff();
        plane.fly();

        Bird bird = new Bird();
        bird.fly();
        bird.buildNest();
    }

}

附源码:

排版源码:


本站由以下主机服务商提供服务支持:

0条评论

发表评论