上机实验:图形的继承机制 java程序

上机实验:图形的继承机制
一) 实验要求:编写Shape类、Rectangle类,Circle类和Triangl e类。
(1)Shape类是父类,其他三个类是子类。
Shape类包含两个属性:x和y,
一个获取面积的getArea()方法;
一个获取周长的getPerimeter()方法;
(2)Rectangle类是Shape类的子类:
Rectangle类包括两个属性: width和 height;
设置setwidth()和setheight();
获取getwidth()和 getheight();
求面积rectangleArea()和周长rectanglePerimeter();
(3)Circle类是Shape类的子类:
Circle类包括一个属性:radius;
设置set Radius();
获取get Radius();
求面积circleArea()和周长circlePerimeter();

package sample;
import java.util.Scanner;

public class Shape {
    int x;
    int y;
    public int getx() {
        return x;
    }
    public void setx(int x) {
        this.x=x;
    }
    public int gety() {
        return y;
    }
    public void sety(int y) {
        this.y=y;
    }
    public double getArea() {
        return 0;
    }
    public double getPerimeter() {
        return 0;
    }
}
    class Rectangle extends Shape {
        private double width;
        private double height;
        public Rectangle(double width,double height) {
            this.width=width;
            this.height=height;
        }
        public void setWidth(double width) {
        this.width=width;
        }
        public double getWidth() {
            return width;
        }
        public void setHeight(double height) {
            this.height=height;
            }
        public double getHeight() {
                return height;
        }
        public double getArea() {
            return width*height;
        }
        public  double getPerimeter() {
            return 2*(width+height);
        }
        public String toString() {
            return"面积是:"+getArea()+"\n周长是:"+getPerimeter();
        }
        
    

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Shape shape=new Shape();
        shape.setx(1);
    }

    }


                 随便写的

写的也差不多了。再补充其他几个子类就行了


package sample;
import java.util.Scanner;
public class Shape {
    int x;
    int y;
    public int getx() {
        return x;
    }
    public void setx(int x) {
        this.x=x;
    }
    public int gety() {
        return y;
    }
    public void sety(int y) {
        this.y=y;
    }
    public double getArea() {
        return 0;
    }
    public double getPerimeter() {
        return 0;
    }
}
    class Rectangle extends Shape {
        private double width;
        private double height;
        public Rectangle(double width,double height) {
            this.width=width;
            this.height=height;
        }
        public void setWidth(double width) {
        this.width=width;
        }
        public double getWidth() {
            return width;
        }
        public void setHeight(double height) {
            this.height=height;
            }
        public double getHeight() {
                return height;
        }
        public double getArea() {
            return width*height;
        }
        public  double getPerimeter() {
            return 2*(width+height);
        }
        public String toString() {
            return"面积是:"+getArea()+"\n周长是:"+getPerimeter();
        }
        class Circle extends Shape{
            private double radius;
            public Circle(double radius) {
                this.radius=radius;
            }
            public void setRadius(double radius) {
                this.radius=radius;
            }
            public double getRadius() {
                return radius;
            }
            public double getArea() {
                return 3.14*radius*radius;
            }
            public double Perimeter() {
                return 3.14*2*radius;
            }
            public String toString() {
                return "面积是:"+getArea()+"\n周长是:"+getPerimeter();
            }
        }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
       Scanner input=new Scanner(System.in);
       system.out.println("请输入圆的半径:");
       double radius=input.nextDouble();
       Shape shape=new Circle(null,radius);
       
    }
    }

主函数这块看着书上的敲了一点不太明白了。就没有继续了。上机实验比课本上面的简单一些。但是我太菜了。还有就是eclipse点击运行怎么总是运行上一次编辑的文件呢?