使用Java完成下面程序

第一题: 1.定义一个Animal类,包含如下行为: eat() 打印"要吃饭" run() 打印"会跑步" sleep() 打印"要睡觉"

2.定义一个Dog类,继承Animal类,重写eat(),run()方法 定义自己特有的行为 : cry() 打印"狗会汪汪叫" 3.定义测试类DogTest, 创建Dog的对象,依次调用eat(),run(),sleep(),cry()方法,打印出如下语句 狗要吃那啥 狗跑的如脱缰的野狗 要睡觉 狗会汪汪叫

Animal类代码如下

复制代码
public class Animal {
    public void eat() {
        System.out.println("要吃饭");
    }
    public void sleep() {
        System.out.println("要睡觉");
    }
    public void run(){
        System.out.println("会跑步");
    }
}

Dog类代码如下

复制代码
public class Dog extends Animal {
    public void eat(){
        System.out.println("狗要吃那啥");
    }
    public void run(){
        System.out.println("狗跑的如脱缰的野马");
    }
    public void cry(){
        System.out.println("狗会汪汪叫");
    }

}

测试类代码如下

复制代码
public class DogTest {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.eat();
        d.run();
        d.sleep();
        d.cry();
    }
}

结果如下

第二题: 1.定义Person类,包含空参、满参构造和以下成员变量: 姓名name 年龄age 生成所有成员变量set/get方法 定义方法:void printPerson(),打印出"我是山顶洞人"

2.定义Student继承于Person类,新增如下属性 学校 school 生成空参、满参构造和set/get方法 定义方法: void printStudent(),先输出"我叫XXX,我今年YYY岁,我现在的学校是:ZZZ" 重写方法:void printPerson(),打印出"我叫XXX,我是山顶洞人"

3.在测试类中,创建并初始化一个Student 对象 s, 调用printStudent()方法,打印出如下语句:

我叫卡特琳娜,我今年19岁,我现在的学校是:战争学院

我叫卡特琳娜,我是山顶洞人

Person类代码如下

复制代码
public class Person {
    String name;
    int age;
    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    void printPerson(){
        System.out.println("我是山顶洞人");
    }

}

Student类代码如下

复制代码
public class Student1 extends Person{
    String school;
    public Student1(){

    }
    public void Student1(String name, int age, String school){

    }
    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }
    void printStudent1(){
        System.out.println("我叫"+name+"我今年"+age+"岁,"+"我现在学校是:"+school);
    }

    @Override
    void printPerson() {
        System.out.println("我叫"+name+",我是山顶洞人");
    }
}

测试类代码如下

复制代码
public class TestStudent1 {
    public static void main(String[] args) {
        Student1 s1 = new Student1();
        s1.name = "卡特琳娜";
        s1.age = 19;
        s1.school = "战争学院";
        s1.printStudent1();
        s1.printPerson();
    }
}

结果如下

第三题: 设计一个形状类Shape,方法:求周长和求面积,属性:圆周率PI

形状类的子类:

Rect(矩形) 属性 width height 重写求面积和周长的方法

Circle(圆形)属性 r 重写求面积和周长的方法

Circle圆形的子类:cylinder(圆柱形),有属性 height,定义方法volume

求圆柱提交 Rect类的子类:Square(正方形) 在测试类中,

1.分别创建以上对象并赋值

2.获取每一个对象,并调用其周长和面积的方法

Shape类代码如下

复制代码
public class Shape {
    public void zhouChang(){

    }
    public void area(){

    }
    final double PI = 3.14;

}

矩形Rect类代码如下

复制代码
public class Rect extends Shape{
    int width;
    int height;


    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getWidth() {
        return width;
    }

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


    public void zhouChang(int width,int height){
        System.out.println("矩形周长为:"+(width+height)*2);
    }
    public void area(int width,int height){
        System.out.println("矩形面积为:"+width*height);
    }
}

圆形Circlel类 代码如下

复制代码
public class Circle extends Shape {
    public int getR() {
        return r;
    }

    public void setR(int r) {
        this.r = r;
    }


    int r;
    public void zhouChang(int r){
        System.out.println("圆的周长为:"+2*PI*r);
    }
    public void area(int r){
        System.out.println("圆的面积为:"+PI*r*r);
    }
}

圆柱形Cylinder类代码如下

复制代码
public class Cylinder extends Circle{

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    int height;
    public void volume(int height,double PI,int r){
        System.out.println("圆柱面积为:"+height*PI*r*r);
    }
}

正方形Square类代码如下

复制代码
public class Square extends Rect {


    public void area(int width, int height){
        System.out.println("正方形面积为:"+width*height);
    }
    public void zhouChang(int width, int height){
        System.out.println("正方形周长为:"+2*(width+height));
    }
}

测试类代码如下

复制代码
public class TestShape {
    public static void main(String[] args) {
        Rect rect = new Rect();
        rect.zhouChang(4,6);
        rect.area(4,6);
        Circle circle = new Circle();
        circle.area(5);
        circle.zhouChang(5);
        Cylinder cylinder = new Cylinder();
        cylinder.area(5);
        cylinder.zhouChang(5);
        Square square = new Square();
        square.area(6,6);
        square.zhouChang(6,6);
    }
}

结果如下