1903021109—方迪琦—Java第十一周作业—继承多态和抽象类

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

 

 

 

 

 

题目1:

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

代码:

父类:
package edu.xj.ff.week11;

public class Person {
	private String name;
	private int birthYear;
	public Person(String name, int birthYear) {
		this.name = name;
		this.birthYear = birthYear;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getBirthYear() {
		return birthYear;
	}
	public void setBirthYear(int birthYear) {
		this.birthYear = birthYear;
	}
}
子类:
package edu.xj.ff.week11;

public class Graduate extends Person {
	private double gpa;
	private int graduateYear;
	public Graduate(String name, int birthYear, double gpa, int graduateYear) {
		super(name, birthYear);
		this.gpa = gpa;
		this.graduateYear = graduateYear;
	}
	public double getGpa() {
		return gpa;
	}
	public void setGpa(double gpa) {
		this.gpa = gpa;
	}
	public int getGraduateYear() {
		return graduateYear;
	}
	public void setGraduateYear(int graduateYear) {
		this.graduateYear = graduateYear;
	}
}
测试类:
package edu.xj.ff.week11;

public class PersonTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Graduate G = new Graduate("李华",2000,86,2022);
		System.out.println("姓名:"+G.getName()+"\n年龄:"+(G.getGraduateYear()-G.getBirthYear())+"\n毕业年份:"+G.getGraduateYear()+"\n成绩:"+G.getGpa());
	}

}

运行结果:

题目2:

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

代码:

父类:
package edu.xj.ff.week11;

public class Shape {
	void draw(){
	}
}
子类Circle(圆):
package edu.xj.ff.week11;

public class Circle extends Shape {
	void draw(){
		System.out.println("Circle.draw()");
	}
}
子类Triangln(三角形):
package edu.xj.ff.week11;

public class Triangln extends Shape {
	void draw(){
		System.out.println("Triangle.draw()");
	}
}
子类Square(正方形):
package edu.xj.ff.week11;

public class Square extends Shape {
	void draw(){
	System.out.println("Square.draw()");	
	}
}
测试类:
package edu.xj.ff.week11;

public class ShapeTest {
	static void doStuff(Shape s){
		s.draw();
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Shape c = new Circle();
		Shape s = new Square();
		Shape t = new Triangln();
		doStuff(c);
		doStuff(t);
		doStuff(s);
	}

}

运行结果:

题目3:

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

  • 提示:抽象

代码:

父类:
package edu.xj.ff.week11;

public abstract class Animal {
	public void bark(){
	}
}
子类Bird(鸟):
package edu.xj.ff.week11;

public class Bird extends Animal {
	public void bark(){
		System.out.println("鸟叫声是:叽叽喳喳");
	}
}
子类Dog(狗):
package edu.xj.ff.week11;

public class Dog extends Animal {
	public void bark(){
		System.out.println("狗叫声是:汪汪汪");
	}
}
测试类:
package edu.xj.ff.week11;

public class AnimalTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Bird B = new Bird();
		Dog D = new Dog();
		B.bark();
		D.bark();
	}

}

运行结果:

题目4:

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

  • 提示:抽象

代码:

父类:
package edu.xj.ff.week11;

public abstract class Shape1 {
	abstract void Area();
}
子类Rectangle(长方形):
package edu.xj.ff.week11;

public class Rectangle extends Shape1 {
	int lenght;
	int height;
	public Rectangle(int lenght, int height) {
		super();
		this.lenght = lenght;
		this.height = height;
	}
	void Area() {
		// TODO Auto-generated method stub
		System.out.println("长方形的面积是:"+lenght*height);
	}

}
子类Triangle(三角形):
package edu.xj.ff.week11;

public class Triangle extends Shape1 {
	int lenght;
	int height;
	public Triangle(int lenght, int height) {
		super();
		this.lenght = lenght;
		this.height = height;
	}
	void Area() {
		// TODO Auto-generated method stub
		System.out.println("三角形的面积是:"+lenght*height/2);
	}

}
测试类:
package edu.xj.ff.week11;

public class Shape1Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Rectangle R = new Rectangle(5,8);
		R.Area();
		Triangle T = new Triangle(5,8);
		T.Area();
	}

}

运行结果:

阅读心得:

1、了解了java中的每一个类都有一个父类,有的显性的表示,有的则隐性表示,但都是继承一Objec,他是所有java类的父类。

2、对多态的概念有了更深入的理解。

3、知道了不是所有的类型都可以相互转换的,只有继承关系的对象之间才可以相互转换。

4、对抽象类的具体用法有了一定的了解。

posted @ 2022-05-14 17:06  ~distance~  阅读(14)  评论(0编辑  收藏  举报