java实验(头歌)--类的继承以及抽象类的定义和使用

文章目录

      • 第一题
      • 第二题
      • 第三题

第一题

java实验(头歌)--类的继承以及抽象类的定义和使用_第1张图片

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.Scanner;

//把main 函数的给替换了
public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double radius = scanner.nextDouble();
        double length = scanner.nextDouble();
        double width = scanner.nextDouble();
        Circle circle = new Circle(radius);
        Rectangle rectangle = new Rectangle(length, width);
        System.out.println(circle.getAcreage());
        System.out.print(rectangle.getAcreage());
    }


abstract class shape{

    public abstract void getAcreage();
}

class Circle implements Shape {
    private double radius;

    public Circle() {
    }

    public Circle(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }


    @Override
    public java.awt.Rectangle getBounds() {
        return null;
    }

    @Override
    public Rectangle2D getBounds2D() {
        return null;
    }

    @Override
    public boolean contains(double x, double y) {
        return false;
    }

    @Override
    public boolean contains(Point2D p) {
        return false;
    }

    @Override
    public boolean intersects(double x, double y, double w, double h) {
        return false;
    }

    @Override
    public boolean intersects(Rectangle2D r) {
        return false;
    }

    @Override
    public boolean contains(double x, double y, double w, double h) {
        return false;
    }

    @Override
    public boolean contains(Rectangle2D r) {
        return false;
    }

    @Override
    public PathIterator getPathIterator(AffineTransform at) {
        return null;
    }

    @Override
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
        return null;
    }

    public double getAcreage() {
        return 3.1415926 * getRadius() * getRadius();
    }
}

class Rectangle implements Shape {
    private double length;
    private double width;

    public double getLength() {
        return length;
    }

    public void setLength(double length) {
        this.length = length;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public Rectangle() {
    }

    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    @Override
    public java.awt.Rectangle getBounds() {
        return null;
    }

    @Override
    public Rectangle2D getBounds2D() {
        return null;
    }

    @Override
    public boolean contains(double x, double y) {
        return false;
    }

    @Override
    public boolean contains(Point2D p) {
        return false;
    }

    @Override
    public boolean intersects(double x, double y, double w, double h) {
        return false;
    }

    @Override
    public boolean intersects(Rectangle2D r) {
        return false;
    }

    @Override
    public boolean contains(double x, double y, double w, double h) {
        return false;
    }

    @Override
    public boolean contains(Rectangle2D r) {
        return false;
    }

    @Override
    public PathIterator getPathIterator(AffineTransform at) {
        return null;
    }

    @Override
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
        return null;
    }

    public double getAcreage() {
        return getLength()*getWidth();
    }
}

第二题

java实验(头歌)--类的继承以及抽象类的定义和使用_第2张图片

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        String birth = scanner.next();
        String position = scanner.next();
        double salary = scanner.nextDouble();
        Salary employee = new Salary(name, birth, position, salary);
        employee.introduction();
    }

/********** Begin *********/
class Employee {
    private String name;
    private String birth;
    private String position;

    public Employee() {
    }

    public Employee(String name, String birth, String position) {
        this.name = name;
        this.birth = birth;
        this.position = position;
    }

    public String getName() {
        return name;
    }

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

    public String getBirth() {
        return birth;
    }

    public void setBirth(String birth) {
        this.birth = birth;
    }

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }
}

class Salary extends Employee {
    private double salary;

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public Salary(String name, String birth, String position, double salary) {
        super(name, birth, position);
        this.salary = salary;
    }

    public void introduction()
    {
        System.out.println("员工姓名:"+getName()+" 出生年月:"+getBirth()+" 职位:"+getPosition()+" 薪水:"+getSalary());
    }
}
/********** End *********/

第三题

java实验(头歌)--类的继承以及抽象类的定义和使用_第3张图片

    public static void main(String[] args) {
        /********** Begin *********/
        Animal cat = new Cat();
        Animal dog = new Dog();
        /********** End *********/
        cat.eat();
        cat.work();
        dog.eat();
        dog.work();
    }

abstract class Animal {
    abstract void eat();
    abstract void work();
}

/********** Begin *********/
class Cat extends Animal {
    @Override
    public void eat()
    {
        System.out.println("吃鱼");
    }

    @Override
    public void work()
    {
        System.out.println("抓老鼠");
    }

}

class Dog extends Animal {
    @Override
    public void eat()
    {
        System.out.println("吃骨头");
    }

    @Override
    public void work()
    {
        System.out.println("看家");
    }

}

你可能感兴趣的:(java实验,java,开发语言)