题目1
* 类Person定义了姓名name,出生年份birthYear,其子类Graduate在继承父类的基础上新增定义了成绩gpa、毕业年份graduateYear。编写测试类,输出毕业生的姓名、年龄、毕业年份、成绩。
- 提示:父类要有构造方法,子类通过super调用父类构造方法。
代码如下:
//父类Person
package 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;
}
}
//子类Graduate
package week11;
public class Graduate extends Person {
int gpa;
int graduateYear;
int nowYear;
public Gradute(String name, double birthYear, int graduateYear, int gpa, int nowYear) {
super(name, birthYear);
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;
}
}
//Test类:
package week11;
public class Test {
public static void main(String[] args) {
gradute g = new gradute("小明",2000,2023,98,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 week11;
public class Shape {
void draw() {}
}
//Circle类
package week11;
public class Circle extends Shape {
void draw() {
System.out.println("Circle.draw()");
}
}
//Triangle类
package week11;
public class Triangle extends Shape {
void draw() {
System.out.println("Triangle.draw");
}
}
// Square类
package week11;
public class Square extends Shape {
void draw() {
System.out.println("Square.draw");
}
}
运行结果如下:
题目3
* 所有的动物都有一个父类Animal,再定义两个子类Bird(鸟)和Dog(狗)继承自Animal,并实现父类中的bark(叫唤)方法。
代码如下:
//Animal类
package week11;
public abstract class Animal {
public abstract void bark();
}
//Bird类
package week11;
public class Bird extends Animal {
public void bark() {
System.out.println("小鸟的叫声:啾啾");
}
}
//Dog类
package week11;
public class Dog extends Animal {
public void bark() {
System.out.println("小狗的叫声是:汪汪");
}
}
//Test类:
package week11;
public class Test1 {
public static void main(String[] args) {
Animal d = new Dog1();
b.bark();
d.bark();
}
}
运行结果如下:
题目4
* 不同几何图形的面积计算公式是不一样的,可是,它们具有的特性是一样的,都具有长和宽这两个属性,也都具有面积计算的方法。根据抽象的概念计算长方形的面积和三角形的面积。
代码如下:
//Shape类
package week11;
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 week11;
public class Square extends Shape {
public Square(int width, int height) {
super(width, height);
}
public double area() {
return width*height;
}
}
//Triangle类:
package week11;
public class Triangle1 extends Shape {
public Triangle1(int width, int height) {
super(width, height);
}
public double area() {
return 0.5*width*height;
}
}
//Test2类:
package week11;
public class Test2 {
public static void main(String[] args) {
Shape s = new Square(6,8);
System.out.println("正方形面积为:"+s.area());
Triangle1 t = new Triangle1(6,8);
System.out.println("三角形面积为:"+t.area());
}
}
运行结果如下:
心得体会:
(1)对抽象类的概念更加深刻,通过abstract 修饰的java类就是抽象类。public abstract class 类名{} --> 抽象类。
(2)抽象类的原由:抽象类实际上就是提供同一种类型事物的公共内容,由抽象类的子类根据自身的实际情况,来实现这个抽象类提供的公共内容。这样子类就不需要创建这个公共内容,只需要继承来重写一下就好,在一定程度上减少了代码的冗余。
(3)抽象类的元素: 实例变量,静态成员变量,构造函数,实例方法,静态方法,[抽象类](可有可无)。
(4)抽象类的具体使用方法:抽象类不能new,借助子类访问抽象类中的实例元素;普通的java类继承了抽象类,就需要重写抽象类中的所有抽象方法,否则将这个普通的java类改成抽象类;抽象类可以继承其他的抽象类,可以不用重写抽象方法;利用上转型对象创建出抽象类对象等等。