项目 

内容

班级链接

19信计班(本)

作业链接

第十一周作业

博客名称

1903021101—闭腾—Java第十一周作业—Java的继承,多态

要求

每道题要有题目,代码(使用插入代码,不会插入代码的自己查资料解决,不要直接截图代码!!),截图(只截运行结果)。

 

 

*扩展阅读:Java中的继承和多态、Java中的抽象类。

题目1:

  • 类Person定义了姓名name,出生年份birthYear,其子类Graduate在继承父类的基础上新增定义了成绩gpa、毕业年份graduateYear。编写测试类,输出毕业生的姓名、年龄、毕业年份、成绩。
  • 提示:父类要有构造方法,子类通过super调用父类构造方法。
  • 代码如下
package xjweek11;

public abstract class Person {
      public String name;
      public double birthYear;
      public Person(String name, double birthYear) {
            super();
            this.name = name;
            this.birthYear = birthYear;
        }
         public String getMessage() {
             return "姓名:" +name;
         }

}




package xjweek11;

public class Graduate extends Person {
    int gpa;
    int graduateYear;
    int nowYear;

    public Graduate(String name, double birthYear, int i, int j, int k) {
        super(name, birthYear);
        // TODO Auto-generated constructor stub
        this.nowYear = nowYear;
        this.gpa = gpa;
        this.graduateYear = graduateYear;
    }
    public String getMessage() {
        return super.getMessage()+"\n毕业年份:"+graduateYear+"\n成绩:"+gpa;
   }
   public double age() {
       double nowYear = 2022;
       return nowYear-birthYear;
}

}





package xjweek11;

public class Test1 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Graduate g = new Graduate("张三",2000,2023,99,2022);
        System.out.println("毕业生信息为:"+g.getMessage());
        System.out.println("年龄为:"+g.age());
    
    }

}
  • 运行截图

题目2:

  • 定义一个基本类Shape,有一个draw方法,定义三个类Circle(圆)、Triangle(三角形)、Square(正方形)都继承于Shape。在测试类中定义一个方法doStuff,传入基本类shape为参数,并调用draw方法。使用父类Shape创建三个类Circle、 Triangle、Square的实例对象并分别作为参数传入draw方法中。
  • 代码如下

  • package xjweek11;
    
    public abstract class Shape {
        
        void draw(){
            
        }
    
    }
    
    
    
    
    package xjweek11;
    
    public class Circle extends Shape {
        void draw(){
            System.out.println("Circle.draw()");
        }
    
    }
    
    
    
    
    
    
    package xjweek11;
    
    public class Triangle extends Shape {
        void draw(){
            System.out.println("Triangle.draw()");
        }
    
    }
    
    
    
    
    
    
    
    
    package xjweek11;
    
    public class Square extends Shape {
        void draw(){
            System.out.println("Spuare.darw()");
        }
    
    }
    
    
    
    
    
    
    
    
    
    
    
    
    package xjweek11;
    
    public class Test3 {
    
        
            // TODO Auto-generated method stub
            static void doStuff(Shape s){
                s.draw();
            }
            public static void main(String[] args) {
                Shape c=new Circle();
                Shape s=new Square();
                Shape t=new Triangle();
                doStuff(c);
                doStuff(s);
                doStuff(t);
        }
    
    }


    运行截图:

  • Java double类型实体类用什么_Java double类型实体类用什么

     

     

  • 题目3:
  • 所有的动物都有一个父类Animal,再定义两个子类Bird(鸟)和Dog(狗)继承自Animal,并实现父类中的bark(叫唤)方法。
  • 提示:抽象
package xjweek11;

public abstract class Animal {
     void bark(){
         
     }

}



package xjweek11;

public class BIrd extends Animal {
    void bark(){
        System.out.println("鸟是这样叫:呀呼!");
    }
}



package xjweek11;

public class Dog extends Animal {
     void bark(){
            System.out.println("狗是这样叫:嗨害嗨!");
        }

}



package xjweek11;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        BIrd b = new BIrd();
        Dog d = new Dog();
        b.bark();
        d.bark();

    }

}

运行截图

  •  
     
  • Java double类型实体类用什么_Java double类型实体类用什么_02


  • 题目4:
  • 不同几何图形的面积计算公式是不一样的,可是,它们具有的特性是一样的,都具有长和宽这两个属性,也都具有面积计算的方法。根据抽象的概念计算长方形的面积和三角形的面积。

 代码如下:

package edu.xj.cxn.week11;

public  abstract class Xingzhuang {
int width;
int height;
public Xingzhuang(int width, int height) {
    super();
    this.width = width;
    this.height = height;
}
public  abstract double area();
}


**Trigon子类:**
package edu.xj.cxn.week11;

public class Trigon extends Xingzhuang {

    public Trigon(int width, int height) {
        super(width, height);
        // TODO Auto-generated constructor stub
    }
    public double area(){
        return (width*height)/2;
}
}

**Rectangle子类:**
package edu.xj.cxn.week11;

public  class Rectangle extends Xingzhuang {

    public Rectangle(int width, int height) {
        super(width, height);
        // TODO Auto-generated constructor stub
    }
 public double area(){
    return width*height;
}
}


**测试类:**
package edu.xj.cxn.week11;

public class Text4 {

    public static void main(String[] args) {
        Rectangle r=new Rectangle(6,5);
        System.out.println("长方形的面积为:"+r.area());
        Trigon t=new Trigon(4,6);
        System.out.println("三角形的面积为:"+t.area());
        // TODO Auto-generated method stub

    }

}
  • 运行截图
  • Java double类型实体类用什么_System_03

  • 阅读心得:
  • 继承和多态是面向对象开发语言中非常重要的一个环节,若使用得当,整个程序的架构将变得非常有弹性,同时可以减少代码的冗余性。