Java--类继承

主要内容

  1. 学生类
  2. 交通工具类
  3. 圆类

一.学生类

具有属性:姓名、年龄、学位。由学生类派生出本科生类和研究生类,本科生类
增加属性:专业,研究生类增加属性:研究方向,每个类都有 show()方法,用于输出属性信息。

1.源代码

代码如下(示例):
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();
 
 }
}

2.结果

在这里插入图片描述

二.交通工具类

属性包括:速度、类别、颜色;方法包括:设置速度、设置颜色、取得类别、取得颜色。
设计一个小车类继承自交通工具类,新增属性:座位数,增加设置和获取座位数的方法,创建小车类对象,为其设置新的颜色和速度,并显示所有属性 信息。

1.源代码

代码如下(示例):
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());
 }
}

2.结果

在这里插入图片描述

三.圆类

具有属性:圆心坐标 x和 y以及圆半径 r,具有设置和获取属性的方法,及计算周长和面积的方法。再设计一个圆柱体类继承自圆类,增加属性:高度,增加了设置和获取高度的方法,及计算表面积和计算体积的方法。创建圆柱体类对象,显示其所有属性信息,计算并显示其面积和体积。

1.源代码

代码如下(示例):
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());
 }
}

2.结果

在这里插入图片描述


总结

以上是今天要讲的内容,学习了类继承。

  • 25
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

K要努力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值