Java面向对象实验 5——继承与多态

一、上机目的

        1.掌握类的声明,对象的创建以及方法的定义和调用。

        2.掌握包机制。

        3.掌握类的继承。

        4 .掌握多态机制。

        5.掌握抽象类与接口的使用。

二、上机内容

1.设计一个Personal类:

(1)该类有实例变量(属性)id、name、age、sex;

(2)一个无参构造函数和一个有参构造函数;

(3)一组用于访问实例变量的set/get方法。

再设计一个测试类TestPersonal,测试该类。

package OOP.work.Test5.T1;

public class Personal {
    private int id;
    private  String name;
    private int age;
    private String sex;

    public Personal() {
    }

    public Personal(int id, String name, int age, String sex) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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 getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "成员有:" +
                "编号是:" + id +
                ", 姓名是:" + name +
                ", 年龄为:" + age +
                ", 性别为:" + sex  ;
    }
}


//测试类

class TestPersonal{
    public static void main(String[] args) {
        /*Personal person=new Personal(01,"镜玄",18,"男");
        System.out.println(person.toString());*/
        Personal p=new Personal();
        p.setId(01);
        p.setName("镜玄");
        p.setAge(18);
        p.setSex("男");
        System.out.println(p.toString());

    }
}

 

 

2.设计一个类Student类,该类从Personal派生,该类有学生学号studentID、所在的系部department、年级grade。新的Student类中有相应的构造函数、查询及输出学生个人信息的getInfo()与print()方法。再设计一个测试类TestStudent,测试该类。

package OOP.work.Test5.T2;

import OOP.work.Test5.T1.Personal;

import java.util.ArrayList;
import java.util.Iterator;

public class Student extends Personal {
    private int studentID;
    private String dapartment;
    private String grade;

    //构造方法, idea可以直接使用快捷键 Alt+Ins(set和get、tostring同理)

    public Student(){

    }

    public Student(int StudentID,String department,String grade) {
        super();
        this.studentID = studentID;
        this.dapartment = dapartment;
        this.grade = grade;
    }

    public Student(int id, String name, int age, String sex, int studentID, String dapartment, String grade) {
        super(id, name, age, sex);
        this.studentID = studentID;
        this.dapartment = dapartment;
        this.grade = grade;
    }

    public int getStudentID() {
        return studentID;
    }

    public void setStudentID(int studentID) {
        this.studentID = studentID;
    }

    public String getDapartment() {
        return dapartment;
    }

    public void setDapartment(String dapartment) {
        this.dapartment = dapartment;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

    public String getInfo(){
        return "学号:"+studentID+",系部:"+dapartment+",年级:"+grade;

     }

     public void print(){
        System.out.println("编号为:"+this.getId()+",姓名:"+this.getName()+",年龄:"+this.getAge()+",性别:"+this.getSex()+'\n'
                +"学号:"+this.getStudentID()+",所在的系部:"+this.getDapartment()+",年级:"+this.getGrade());
     }
}

//测试类
class TestStudent{
    public static void main(String[] args) {
        Student stu1=new Student();
        stu1.setStudentID(20102);
        stu1.setDapartment("电子与信息工程学院");
        stu1.setGrade("计算机Z2011");
        stu1.setId(01);
        stu1.setName("镜玄");
        stu1.setAge(18);
        stu1.setSex("男");

        System.out.println("查询结果为:"+stu1.getInfo());
        System.out.print("输出结果为:");
        stu1.print();
    }
}

 

 

3.设计一个Pencil类,RubberPencil类是Pencil的子类并实现了Erase接口。

1)接口Erase包含方法

       public void erase(String s); 

    2)Pencil类:具有以下属性和方法:

      属性type:String类型,表示Pencil的品牌

      方法:toString(String r):及构造函数,设置type及取得type值的方法

    3)RubberPencil类:(可根据自己的需要添加其他方法和属性)

       重写父类的toString()方法,输出橡皮铅笔的相关属性,构造函数。

   4)Test类作为主类完成测试功能:其中要求通过使用造型等功能输出Pencil、RubberPencil的相关信息。

package OOP.work.Test5.T3;

//父类Pencil
public class Pencil {
    private String type; //pencil的品牌

    //构造方法
    public Pencil() {
    }

    public Pencil(String type) {
        this.type = type;
    }
    //getter和setter方法
    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String toString() {
        return "Pencil{" +
                "type='" + type + '\'' +
                '}';
    }

    //取得type的方法
    public String getInfo(){
        return type;
    }
    //设置type

}

//子类RubberPencil
class RubberPencil extends Pencil implements Erase{

    @Override
    public void erase(String s) {
        System.out.println(s+"已擦除");
    }

    public void write(String s) {
        System.out.println("用" + this + "写了" + s);
    }

    public RubberPencil(){

    }

    public RubberPencil(String type) {
        super(type);
    }
    @Override
    public String toString(){
        return this.getType();
    }
}

//接口Erase
interface Erase{
     void erase(String s);
}

//测试类Test
class Test{
    public static void main(String[] args) {
        Pencil rp1 =new RubberPencil("晨光");
        Pencil p1 =new Pencil("思量");
        //输出信息
        System.out.println(rp1);
        System.out.println(p1);
        //调用方法
        //向下转型
        if (rp1 instanceof RubberPencil) {
            RubberPencil rubberPencil1 = (RubberPencil) rp1;
            rubberPencil1.write("思量岛");
            rubberPencil1.erase("思量岛");
        }
    }
}

 

 

4.完成以下功能

(1)包cs2022.vo中包含如下内容:

  父类point:包括描述点坐标的私有成员x和y;对成员的set/get方法;构造方法(有参和无参两种);显示点位置(坐标)的toString方法。

Point类的子类Circle:包括描述半径的私有成员radius;对成员radius的set/get方法;构造方法(无参、1个参数两种);方法getArea()获得面积,方法toString()输出位置和半径信息(调用父类toString)。

  Point类的子类Rectangle:包括描述长和宽的私有成员l和w;对成员l和w的set/get方法;构造方法(无参、2个参数两种);方法getArea()获得面积,方法toString()输出位置和长宽信息(调用父类toString)。

父类Point:

package OOP.work.Test5.T4.v0;

public class Point {
    private double x;
    private double y;

    public void setX(double x) {
        this.x=x;
    }

    public double getX() {
        return x;
    }

    public void setY(double y) {
        this.y=y;
    }

    public double getY() {
        return y;
    }

    public Point() {

    }

    public Point(double x,double y) {
        this.x=x;
        this.y=y;
    }

    @Override
    public String toString() {
        return "坐标(" + x + ", " + y + ")";
    }
}

子类Circle:

package OOP.work.Test5.T4.v0;

public class Circle extends Point{
    private double radius;

    public void setRadius(double radius) {
        this.radius=radius;
    }

    public double getRadius() {
        return radius;
    }

    public Circle() {

    }

    public Circle(double radius) {
        this.radius=radius;
    }

    public Circle(double x,double y,double radius) {
        super(x, y);
        this.radius=radius;
    }

    public double getArea() {
        return getRadius()*getRadius()*Math.PI;
    }

    public String toString() {
        return super.toString()+ "半径为:"+radius+",面积为:"+getArea();
    }
}

子类Rectangle:

package OOP.work.Test5.T4.v0;

public class Rectangle extends Point{
    private double l;
    private double w;

    public Rectangle() {

    }

    public Rectangle(double l,double w) {
        this.l=l;
        this.w=w;
    }

    public Rectangle(double x,double y,double l,double w) {
        super(x,y);
        this.l=l;
        this.w=w;
    }

    public void setL(double l) {
        this.l=l;
    }

    public double getL() {
        return l;
    }

    public void setW(double w) {
        this.w=w;
    }

    public double getW() {
        return w;
    }

    public double getArea() {
        return getL()*getW();
    }

    public String toString() {
        return super.toString()+"长为:"+l+",宽为:"+w+",面积为:"+getArea();
    }
}

 

 

(2)包cs2022.v1中包含如下内容:

  父类: Shape,其中Point类对象point为Shape类的私有属性成员;写出point的set/get方法;Shape类的构造方法;toString方法;子类:Rectangle、Circle继承了Shape类,写出其相应属性的set/get方法;类的构造方法;toString方法;getArea方法。

父类Shape:

package OOP.work.Test5.T4.v1;

import OOP.work.Test5.T4.v0.Point;

public class Shape {
    private Point point;

    public void setPoint(Point point) {
        this.point=point;
    }


    public Point getPoint() {
        return point;
    }

    public Shape() {

    }

    public Shape(Point point) {
        this.point=point;
    }

    public String toString() {
        return "坐标("+point.getX()+","+point.getY()+")";
    }
}

子类Rectangle:

package OOP.work.Test5.T4.v1;

import OOP.work.Test5.T4.v0.Point;

public class Rectangle extends Point {
    private double l;
    private double w;

    public Rectangle() {
    }

    public Rectangle(double l, double w) {
        this.l = l;
        this.w = w;
    }

    public Rectangle(double x, double y, double l, double w) {
        super(x, y);
        this.l = l;
        this.w = w;
    }

    public void setL(double l) {
        this.l=l;
    }

    public double getL() {
        return l;
    }

    public void setW(double w) {
        this.w=w;
    }

    public double getW() {
        return w;
    }

    public double getArea() {
        return l*w;
    }

    public String toString() {
        return super.toString()+"长:"+l+",宽:"+w+",面积:"+getArea();
    }
}

子类:Circle:

package OOP.work.Test5.T4.v1;

import OOP.work.Test5.T4.v0.Point;

public class Circle extends Shape{
    private double radius;

    public Circle() {

    }

    public Circle(double radius) {
        this.radius=radius;
    }

    public Circle(double x,double y,double radius) {
        setPoint(new Point(x, y));
        this.radius=radius;
    }

    public void setRadius(double radius) {
        this.radius=radius;
    }

    public double getradius() {
        return radius;
    }

    public double getArea() {
        return getradius()*getradius()*Math.PI;
    }

    public String toString() {
        return super.toString()+"半径为:"+radius+",面积为:"+getArea();
    }
}

 

 

(3)包cs2022.v2中包含如下内容:

   抽象类Shape,其中Point类对象为Shape类的属性成员,方法getAera()为抽象方法,

   子类:Rectangle、Circle类继承了Shape类,在子类中,重写了getAera()方法。

抽象类Shape:

子类Rectangle:

子类Circle:

(4)包2022.v3中包含如下内容:

    接口:  Shape,接口中有getAera()和getC()两个抽象方法。

接口的实现类:  Rectangle、Circle类实现了Shape接口中的getAera()和getC()方法,Point类对象为它们的属性成员。

接口类Shape:

package OOP.work.Test5.T4.v3;

public interface Shape {
    public double getArea();

    public double getC();

}

Rectangle类:

package OOP.work.Test5.T4.v3;

import OOP.work.Test5.T4.v0.Point;

public class Rectangle implements Shape{
    private Point point;
    private double l;
    private double w;

    public Rectangle() {

    }

    public Rectangle(double l,double w) {
        this.l=l;
        this.w=w;
    }

    public Rectangle(double x,double y,double l,double w) {
        point =new Point(x,y);
        this.l=l;
        this.w=w;
    }

    public void setPoint(Point point) {
        this.point=point;
    }

    public Point getPoint() {
        return point;
    }

    public void setL(double l) {
        this.l=l;
    }

    public double getL() {
        return l;
    }

    public void setW(double w) {
        this.w=w;
    }

    public double getW() {
        return w;
    }

    public double getArea() {
        return l*w;
    }

    public double getC() {
        return 2*(l+w);
    }

    public String toString() {
        return "坐标("+point.getX()+","+point.getY()+")"+"长为:"+l+",宽为:"+w;
    }
}

Circle类:

package OOP.work.Test5.T4.v3;

import OOP.work.Test5.T4.v0.Point;

public class Circle implements Shape{
    private Point point;
    private double radius;

    public Circle() {

    }

    public Circle(double radius) {
        this.radius=radius;
    }

    public Circle(double x,double y,double radius) {
        point=new Point(x,y);
    }

    public void setPoint(Point point) {
        this.point=point;
    }

    public Point getPoint() {
        return point;
    }

    public void setRadius(double radius) {
        this.radius=radius;
    }

    public double getRadius() {
        return radius;
    }

    public double getArea() {
        return Math.PI*getRadius()*getRadius();
    }

    public double getC() {
        return 2*Math.PI*getArea();
    }

    public String toString() {
        return "坐标("+point.getX()+","+point.getY()+")"+"半径为:"+getRadius();
    }
}

 

 

(5)包 cs2022.factor中包含如下内容:

    测试类:

    Test.java

package OOP.work.Test5.T4.factor;

import OOP.work.Test5.T4.v0.Circle;
import OOP.work.Test5.T4.v0.Point;
import OOP.work.Test5.T4.v0.Rectangle;

public class Test {
    public static void main(String[] args) {
        Point p=new Point(2.5, 4.0);
        System.out.println(p.toString());

        p=new Circle(1.2, 3.5, 8);
        System.out.println(p.toString());

        p=new Rectangle(1.5, 2.0, 9.6, 4.2);
        System.out.println(p.toString());
    }
}

    TestV1.java

package OOP.work.Test5.T4.factor;

import OOP.work.Test5.T4.v0.Point;
import OOP.work.Test5.T4.v1.Circle;
import OOP.work.Test5.T4.v1.Shape;

public class TestV1 {
    public static void main(String[] args) {
        Point p=new Point(4.5, 6.2);
        Shape s=new Shape(p);
        System.out.println(s.toString());

        s=new Circle(2.5, 6.0, 5);
        System.out.println(s.toString());


        /*s=new Rectangle(1.6, 5.0, 9.6, 6);
        System.out.println(s.toString());*/

        p=s.getPoint();
        System.out.println(p.toString());
    }
}

    TestV2.java

package OOP.work.Test5.T4.factor;

import OOP.work.Test5.T4.v2.Circle;
import OOP.work.Test5.T4.v2.Rectangle;
import OOP.work.Test5.T4.v2.Shape;

public class TestV2 {
    public static void main(String[] args) {
        Shape s=new Circle(3.0, 4.5, 6);
        System.out.println(s.toString()+",面积为:"+s.getArea());
        s=new Rectangle(3.8, 2, 12, 4);
        System.out.println(s.toString()+",面积为:"+s.getArea());
    }
}

    TestV3.java

package OOP.work.Test5.T4.factor;

import OOP.work.Test5.T4.v3.Circle;
import OOP.work.Test5.T4.v3.Rectangle;
import OOP.work.Test5.T4.v3.Shape;

public class TestV3 {
    public static void main(String[] args) {
        Shape s=new Circle(5, 8, 9);
        System.out.println(s.toString()+",面积为:"+s.getArea()+",周长为:"+s.getC());
        s=new Rectangle(3, 6, 20, 11);
        System.out.println(s.toString()+",面积为:"+s.getArea()+",周长为:"+s.getC());
    }
}

分别是上述包中的类的测试示例,并使用向上转型的方法,将子类转换为父类(或接口)的对象,通过父类(或接口)对象,引用父类(或接口)定义且被重写的相关的方法。

 

最后一题主要是誊记另外一个博主的文章:面向对象编程练习_1uuue的博客-CSDN博客_面向对象作业

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值