java编程 类的继承及多态

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java编程 类的继承及多态相关的知识,希望对你有一定的参考价值。

实验目的: 掌握抽象类、类的继承、组合、多态及塑型。
实验内容:定义一个抽象类:车类(Vehicle);
由Vehicle类派生出轿车类(Car)及公共汽车类(Bus);
定义汽车工厂类VehicleFactory:设置方法(makeVehicle())根据给定的发动机装配一辆车(Vehicle对象)。

要求: 发动机类有一个属性:马力(horsepower),范围1—100,大于50只能用于Bus,小于等于50只能用于Car;生成时根据马力值自动选择生产相应的车型(Car或Bus)。
车类有三个属性:发动机、速度(speed)、总开车里程数(mileage);
多个方法:一个是drive(),用于计算开车里程数,使用Math类中的随机数生成1-6的随机数表示每次开车的时间time并显示,乘上速度speed得到本次开车里程(显示)再加原来的里程数;
一个是calGC(),用于计算总耗油量:Car为里程数乘以2,Bus为里程数乘以3再加20;
并设置相应的set及get方法。

编写测试类,任意给定一个发动机,通过汽车工厂装配一辆车,执行两次drive()方法;
最后显示这辆车的所有信息:
发动机、速度、总开车里程数及总耗油量。(通过toString()方法)。

共六个类:Vehicle.java(车类), Car.java(轿车类), Bus.java(公共汽车类), Engine.java(发动机), VehicleFactory.java(汽车工厂) 和 VehicleTest.java(测试类),具体代码分别如下:

***************************************************************

abstract class Vehicle
Engine engine;
double speed;
double mileage;

public Vehicle()


public Vehicle(Engine engine)
this.engine = engine;


public Vehicle(Engine engine, double mileage)
this.engine = engine;
this.mileage = mileage;


public Vehicle(Engine engine, double speed, double mileage)
this.engine = engine;
this.speed = speed;
this.mileage = mileage;


public Engine getEngine()
return engine;


public void setEngine(Engine engine)
this.engine = engine;


public double getSpeed()
return speed;


public void setSpeed(double speed)
this.speed = speed;


public double getMileage()
return mileage;


public void setMileage(double mileage)
this.mileage = mileage;


public double drive()
this.setMileage(speed * (Math.round(Math.random() * 6)) + mileage);
return mileage;


public abstract double calGC();

public String toString()
return "Type: " + this.getClass().getName() + ", Horsepower: "
+ engine.horsepower + ", Speed: " + speed + ", Mileage: "
+ mileage + ", Gas consumed: " + calGC();




***************************************************************

public class Car extends Vehicle
public Car()



public Car(Engine engine)
this.engine = engine;


public Car(Engine engine, double mileage)
this.engine = engine;
this.mileage = mileage;


public Car(Engine engine, double speed, double mileage)
this.engine = engine;
this.speed = speed;
this.mileage = mileage;


public double calGC()
return mileage * 2;



***************************************************************

public class Bus extends Vehicle
public Bus()



public Bus(Engine engine)
this.engine = engine;


public Bus(Engine engine, double mileage)
this.engine = engine;
this.mileage = mileage;


public Bus(Engine engine, double speed, double mileage)
this.engine = engine;
this.speed = speed;
this.mileage = mileage;


public double calGC()
return mileage * 3 + 20;



***************************************************************

public class Engine
double horsepower;

public Engine()
this.horsepower = 1;


public Engine(double horsepower)
this.horsepower = horsepower;


public double getHorsepower()
return horsepower;


public void setHorsepower(double horsepower)
this.horsepower = horsepower;




***************************************************************

public class VehicleFactory
public static Vehicle makeVehicle(Engine e)
if ((1 <= e.getHorsepower()) && e.getHorsepower() <= 50)
return new Car(e, 0);
else if ((50 < e.getHorsepower()) && e.getHorsepower() <= 100)
return new Bus(e, 0);

return null;



***************************************************************

public class VehicleTest
public static void main(String[] args)
// 新建一辆马力28, 速度50的车
Engine e1 = new Engine(28);
Vehicle v1 = VehicleFactory.makeVehicle(e1);
v1.setSpeed(50);
v1.drive();
v1.drive();
System.out.println("第一辆车:");
System.out.println(v1);
// 新建一辆马力88, 速度88的车
Engine e2 = new Engine(88);
Vehicle v2 = VehicleFactory.makeVehicle(e2);
v2.setSpeed(88);
v2.drive();
v2.drive();
System.out.println("第二辆车:");
System.out.println(v2);



***************************************************************

运行结果如下:

第一辆车:
Type: Car, Horsepower: 28.0, Speed: 50.0, Mileage: 200.0, Gas consumed: 400.0
第二辆车:
Type: Bus, Horsepower: 88.0, Speed: 88.0, Mileage: 616.0, Gas consumed: 1868.0

***************************************************************
参考技术A import java.util.Random;
/**
* 抽象车类
*/
public abstract class Vehicle
private Engine engine;
private int speed;
private int mileage;
/**
* 构造该汽车
*/
public Vehicle(int speed, Engine engine)
this.speed = speed;
this.engine = engine;


public Engine getEngine()
return engine;


public void setEngine(Engine engine)
this.engine = engine;


public int getSpeed()
return speed;


public void setSpeed(int speed)
this.speed = speed;


public int getMileage()
return mileage;


public void setMileage(int mileage)
this.mileage = mileage;

/**
* 开车返回里程数
*/
public int drive()
int time = new Random().nextInt(6);
setMileage(getMileage()+time*getSpeed());
return getMileage();

/**
* 抽象方法,子类实现。获取耗油量
*/
public abstract int calGC();

@Override
public String toString()
// TODO Auto-generated method stub
String str = "发动机:"+getEngine().getHorsepower()+";速度:"+getSpeed()+";总开车里程数:"+getMileage();
return str;


public static void main(String[] args)
Engine engine = new Engine(70);
Vehicle vehicle = new VehicleFactory().makeVehicle(engine);
System.out.println(vehicle.drive());
System.out.println(vehicle.drive());
System.out.println(vehicle);



/**
* 发动机类
*/
class Engine
private int horsepower;

public Engine(int horsepower)
this.horsepower = horsepower;


public int getHorsepower()
return horsepower;



/**
* bus 类
*/
class Bus extends Vehicle
public Bus(int speed, Engine engine)
super(speed,engine);

@Override
public int calGC()

return getMileage()*3+20;

@Override
public String toString()
String str = super.toString();
str = str+";总耗油量:"+getMileage()*3+20;
return str;



/**
* car类
*/
class Car extends Vehicle
public Car(int speed, Engine engine)
super(speed, engine);

@Override
public int calGC()
return getMileage()*2;

@Override
public String toString()
String str = super.toString();
str = str+";总耗油量:"+getMileage()*2;
return str;


/**
* 工厂类
*/
class VehicleFactory
public Vehicle makeVehicle(Engine engine)
int eng = engine.getHorsepower();
if(eng>100 || eng<0)
System.out.println("该发动机不合格");
return null;


if(eng>50)
return new Bus(50, engine);
else
return new Car(70, engine);




代码附上, 有不懂的M我本回答被提问者采纳
参考技术B public abstract class Vehicle
private Engine engine;
private double speed;
private double mileage;

public Engine getEngine()
return engine;

public void setEngine(Engine engine)
this.engine = engine;

public double getSpeed()
return speed;

public void setSpeed(double speed)
this.speed = speed;

public void setMileage(double mileage)
this.mileage = mileage;

public double getMileage()
return mileage;

public Vehicle(Engine engine)
this.engine = engine;


public void driver()
double time = Meth.random()*6;
System.out.println("Time:" + time);
speed = Meth.random()*200;
System.out.println("Mile:" + speed*time);
mileage += speed*time;


public abstract double calGC():


public class Car extends Vehicle
public Car(Engine engine)
super(engine);

public double calGC()
return mileage*2;



public class Bus extends Vehicle
public Bus(Engine engine)
super(engine);

public double calGC()
return mileage*3+20;



public class VehicleFactory
public static Vehicle makeVehicle(Engine engine)
if(engine.getHorsepower > 50)
return new Bus();
else
return new Car();




public class Engine
public Engine(int horsepower)
this.horsepower = horsepower;

private int horsepower;
public int getHorsepower()
return horsepower;

public void setHorsepower(int horsepower)
this.horsepower = horsepower;



public class test
public static void main(String[] args)
Vehicle v = VehicleFactroy.makeVehicle(new Engine(80));
v.drive;
v.drive;
System.out.println("Mileage:" + v.getMileage());

C++笔记--面向对象(OOP)编程基础--类的继承及多态

目录

继承

继承基本概念

访问权限

继承中的构造和析构 

继承与组合混搭情况下,构造和析构调用原则

多继承(摒弃,忘记)

赋值兼容性原则


继承

继承基本概念

访问权限

C++中的类成员访问级别(public、private、protected)

1、需要被外界访问的成员直接设置为public

2、只能在当前类中访问的成员设置为private

3、只能在当前类和子类中访问的成员设置为protected,protected成员的访问权限介于public和private之间。

父类成员访问级别

public

proteced

private

public

public

proteced

private

proteced

proteced

proteced

private

private

private

private

private

判断某一句话,能否被访问

1)看调用语句,这句话写在子类的内部、外部

2)看子类如何从父类继承(public、private、protected)

3)看父类中的访问级别(public、private、protected)

继承中的构造和析构 

1、子类对象在创建时会首先调用父类的构造函数

2、父类构造函数执行结束后,执行子类的构造函数

3、当父类的构造函数有参数时,需要在子类的初始化列表中显示调用

4、析构函数调用的先后顺序与构造函数相反

继承与组合混搭情况下,构造和析构调用原则

原则:

   先构造父类,再构造成员变量、最后构造自己

  先析构自己,在析构成员变量、最后析构父类

  先构造的对象,后释放

示例

#include <iostream>
#include "stdlib.h"
#include "stdio.h"

using namespace std;

class A {
    private:
        char *p;
    public:
        A(char *p) {
            int p_len = strlen(p);
            this->p = (char *)malloc(p_len);
            cout << "构造A..." << endl;
        }
        ~A() {
            if(p != NULL) {
                free(p);
                p = NULL;
            }
            cout << "析构A..." << endl;
        }

};

class B : public A {
    public:
        B(char *p) : A(p) {
            cout << "构造B..." << endl;
        }
        ~B() {
            cout << "构造B..." << endl;
        }
};

int main() {

    B b1("abcd");

    return 0;
}

结果

构造A...
构造B...
构造B...
析构A...

多继承(摒弃,忘记)

多继承的二义性:多继承带来的便利性远远小于软件维护、软件架构复杂性

赋值兼容性原则

子类对象可以当作父类对象使用

子类对象可以直接赋值给父类对象

子类对象可以直接初始化父类对象

父类指针可以直接指向子类对象

父类引用可以直接引用子类对象

总结:子类就是特殊的父类 (base *p = &child;)

以上是关于java编程 类的继承及多态的主要内容,如果未能解决你的问题,请参考以下文章

Java编程 实现类的继承与多态 写一个动物类。成员变量为动物的种类,成员方法是动物叫声。

Java 继承多态与类的复用

C++编程题 关于 继承和多态 的

JAVA基础整理-50.Java继承和多态

2017.10.10 java程序设计-------继承与多态

Java多态的理解