具有属性:姓名、年龄、学位。由学生类派生出本科生类和研究生类,本科生类
增加属性:专业,研究生类增加属性:研究方向,每个类都有 show()方法,用于输出属性信息。
package test;
class Student1{
private String name;
private int age;
private String degree;
public Student1(String name, int age, String degree) {
super();//Java 的规定:子类继承父类,子类的构造方法必须调用
//super()即父类的构造方法,而且必须放在构造方法的第一行
this.name = name;
this.age = age;
this.degree = degree;
}
public Student1() {
super();
}
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;
}
public String getDegree() {
return degree;
}
public void setDegree(String degree) {
this.degree = degree;
}
public void show(){
System.out.println("姓名:" + this.getName() + " 年龄:" + this.getAge()
+ " 学位:" + this.getDegree() );
}
}
class Undergraduate extends Student1{ //本科类
private String specialty;//专业
public String getSpecialty() {
return specialty;
}
public void setSpecialty(String specialty) {
this.specialty = specialty;
}
public Undergraduate(String name, int age, String degree, String specialty)
{
super(name, age, degree);
this.specialty = specialty;
}
public void show(){
System.out.println("姓名:" + this.getName() + " 年龄:" + this.getAge()
+ " 学位:" + this.getDegree() + " 专业:" + this.getSpecialty());
}
}
class Graduate extends Student1{ //研究生类
private String direction;
public String getDirection() {
return direction;
}
public void setDirection(String direction) {
this.direction = direction;
}
public Graduate(String name, int age, String degree, String direction) {
super(name, age, degree);
this.direction = direction;
}
public void show(){
System.out.println("姓名:" + this.getName() + " 年龄:" + this.getAge()
+ " 学位:" + this.getDegree() + " 研究方向:" + this.getDirection());
}
}
public class student {
public static void main(String[] args) {
Undergraduate stu1=new Undergraduate("张三",20,"本科","物联网工程");
Graduate stu2=new Graduate("李四",24,"硕士","计算机科学与技术");
stu1.show();
stu2.show();
}
}
属性包括:速度、类别、颜色;方法包括:设置速度、设置颜色、取得类别、取得颜色。
设计一个小车类继承自交通工具类,新增属性:座位数,增加设置和获取座位数的方法,创建小车类对象,为其设置新的颜色和速度,并显示所有属性 信息。
class transport {
private int speed;
private String classify;
private String color;
transport(int speed,String classify,String color){
this.speed=speed;
this.classify=classify;
this.color=color;
}
public int getspeed() {
return speed;
}
public void setspeed(){
this.speed=speed;
}
public String getclassify() {
return classify;
}
public void setclassify(){
this.classify=classify;
}
public String getcolor() {
return color;
}
public void setcolor(){
this.color=color;
}
public void show(){
System.out.println("速度 "+this.getspeed()+" 类别
"+this.getclassify()+" 颜色 "+this.getcolor());
}
public static void main(String[] args) {
car c1 = new car(100, "宝马", "白色", 4);
c1.show();
}
}
class car extends transport{
private int number;
public int getnumber() {
return number;
}
public void setnumber(){
this.number=number;
}
car(int speed,String classify,String color,int number){
super(speed,classify,color);
this.number=number;
}
public void show(){
System.out.println("速度 "+this.getspeed()+" 类别
"+this.getclassify()+" 颜色 "+this.getcolor()+" 座位数
"+this.getnumber());
}
}
具有属性:圆心坐标 x和 y以及圆半径 r,具有设置和获取属性的方法,及计算周长和面积的方法。再设计一个圆柱体类继承自圆类,增加属性:高度,增加了设置和获取高度的方法,及计算表面积和计算体积的方法。创建圆柱体类对象,显示其所有属性信息,计算并显示其面积和体积。
package test;
class Circle {
double x;
double y;
double r;
Circle(double x, double y, double r) {
this.x = x;
this.y = y;
this.r = r;
}
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
public void setR(double r) {
this.r = r;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getR() {
return r;
}
public double area() {
return r * r * Math.PI;
}
public double perimeter() {
return 2 * r * Math.PI;
}
public void show() {
System.out.print("x=" + x + ", y=" + y + ", Radius=" + r);
}
}
class Cylinder extends Circle {
double h;
Cylinder(double x, double y, double r, double h) {
super(x, y, r);
this.h = h;
}
public void setH(double h) {
this.h = h;
}
public double getH() {
return h;
}
public double area() {
return perimeter() * h + super.area() * 2;
}
public double volume() {
return super.area() * h;
}
public static void main(String[] args) {
Cylinder cylinder = new Cylinder(2, 3, 4, 5);
cylinder.show();
System.out.println(", Height=" + cylinder.getH());
System.out.println("面积=" + cylinder.area());
System.out.println("体积=" + cylinder.volume());
}
}
以上是今天要讲的内容,学习了类继承。