1903021136—张瑞—java第十一周作业—JAVA中的继承,多态和抽象类的运用

项目 内容
班级博客链接 19信计班
作业链接 第十一周作业
博客名称 1903021136—张瑞—java第十一周作业—JAVA中的继承,多态和抽象类的运用
要求 每道题要有题目,代码(使用插入代码,不会插入代码的自己查资料解决,不要直接截图代码!!),截图(只截运行结果)

题目1:

  • 类Person定义了姓名name,出生年份birthYear,其子类Graduate在继承父类的基础上新增定义了成绩gpa、毕业年份graduateYear。编写测试类,输出毕业生的姓名、年龄、毕业年份、成绩。

  • 提示:父类要有构造方法,子类通过super调用父类构造方法。
  •  代码如下:
    父类:package edu.xj.zr.week11;
    
    public 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 edu.xj.zr.week11;
    
    public class Graduate extends Person {
    	int gpa;
        int graduateYear;
        int nowYear;
    
    	public Graduate(String name, double birthYear, int graduateYear, int gpa, int nowYear) {
    		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 edu.xj.zr.week11;
    
    public class Test {
    	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方法中。
    • 提示:多态
    • 代码如下:
      Shape类:
      package edu.xj.week11;
      public class Shape {
      	void draw() {}
      }
      
      Circle(圆)
      package edu.xj.week11;
      public class Circle extends Shape {
      	void draw() {
      		System.out.println("Circle.draw()");
      	}
      }
      
      Triangle(三角形)
      package edu.xj.week11;
      public class Triangle extends Shape {
      	void draw() {
      		System.out.println("Triangle.draw");
      	}
      }
      
      Square(正方形)
      package edu.xj.week11;
      public class Square extends Shape {
      	void draw() {
      		System.out.println("Square.draw");
      	}
      }
    运行结构如图:
  • 题目3:

    • 所有的动物都有一个父类Animal,再定义两个子类Bird(鸟)和Dog(狗)继承自Animal,并实现父类中的bark(叫唤)方法。

    • 提示:抽象
    • 运行代码:
      Animal:
      package edu.week11.xj.zr;
      
      public abstract class Animal {
      	public abstract void bark();
      	}
      鸟类:
      package edu.week11.xj.zr;
      
      public class Bird extends Animal {
      
      	@Override
      	public void bark() {
      		// TODO Auto-generated method stub
      		System.out.println("小鸟叽叽喳喳");
      	}
      }
      狗类:
      package edu.week11.xj.zr;
      
      public class Dog extends Animal {
      
      	@Override
      	public void bark() {
      		// TODO Auto-generated method stub
      		System.out.println("小狗汪汪汪汪");
      	}
      
      	}
      测试类:
      package edu.week11.xj.zr;
      
      public class TestAnimal {
      	public static void main(String[] args) {
      		// TODO Auto-generated method stub
      		Animal b = new Bird();
      		Animal d = new Dog();
      		b.bark();
      		d.bark();
      	}
      }

      运行结果如图:

    • 题目4:

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

      • 提示:抽象
      • 运行代码:
        Shape:
        package edu.week11.xj.zr;
        
        public abstract class Shape {
        	public int width;
        	public int height;
        	public Shape(int width, int height) {
        		super();
        		this.width = width;
        		this.height = height;
        	}
        	public abstract double area();
        }
        
        Square:
        package edu.week11.xj.zr;
        
        public class Square extends Shape {
        	public Square(int width, int height) {
        		super(width, height);
        		// TODO Auto-generated constructor stub
        	}
        	public double area() {
        		return width*height;
        	}
        
        }
        Triangle:
        package edu.week11.xj.zr;
        
        public class Triangle extends Shape {
        	public Triangle(int width, int height) {
        		super(width, height);
        		// TODO Auto-generated constructor stub
        	}
        	public double area() {
        		return 0.5*width*height;
        	}
        }
         TestShape:
        package edu.week11.xj.zr;
        
        public class TestShape {
        	public static void main(String[] args) {
        		// TODO Auto-generated method stub
        		Shape s = new Square(2,8);
        		System.out.println("正方形面积为:"+s.area());
        		Triangle t = new Triangle(4,8);
        		System.out.println("三角形面积为:"+t.area());
        	}
        
        }

        运行结果如图:

posted @ 2022-05-15 19:24  19信计张瑞  阅读(7)  评论(0编辑  收藏  举报