| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 编程论坛
大量收QQ微信精准粉/交友粉,非诚勿扰千里之行 始于足下
共有 451 人关注过本帖
标题:Java类的继承,该怎么按要求更改?
只看楼主 加入收藏
脚丫很香
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2018-5-8
结帖率:100%
  已结贴   问题点数:15  回复次数:5   
Java类的继承,该怎么按要求更改?
代码如下:import java.util.Scanner;
public class Test {
    public  static void main(String args[])
    {
        Scanner scn=new Scanner(System.in);
        System.out.println("请选择功能");
        int t=6,i,m,n;
        //
        Bus bus[]=new Bus[2];
        Route route[]=new Route[2];
        Employee employee[]=new Employee[2];
        Passenger passenger[]=new Passenger[2];
        //
        for(i=0;i<=1;i++)
        {bus[i]=new Bus();
            route[i]=new Route();
            employee [i]=new Employee();
            passenger[i]=new Passenger();}
        //
        while(t!=0) {
            System.out.println("1.车辆管理");
            System.out.println("2.线路管理");
            System.out.println("3.员工管理");
            System.out.println("4.乘客管理");
            System.out.println("5.输出信息");
            t=scn.nextInt();
            //
            switch(t) {
            //
            case 1:{System.out.println("1.添加");
                System.out.println("2.修改");
                System.out.println("3.删除");
                m=scn.nextInt();
                switch(m)
                {//
                case 1:{for(i=0;i<=1;i++)
                if(bus[i].type==null) break;
                if(i!=2) bus[i].Scn();
                else System.out.println("信息已满");}break;
                //添加车辆信息
                case 2:{n=scn.nextInt();
                if(bus[n-1].type==null)System.out.println("所选对象为空");
                else bus[n-1].Scn();}break;
                //修改车辆信息
                case 3:{n=scn.nextInt();
                bus[n-1].del();}break;
                }
                //删除车辆信息
            }break;
            //车辆管理
            case 2:{System.out.println("1.添加");
            System.out.println("2.修改");
            System.out.println("3.删除");
            m=scn.nextInt();
            switch(m)
            {//
            case 1:{for(i=0;i<=1;i++)
            if(route[i].station==null) break;
            if(i!=2) route[i].Scn();
            else System.out.println("信息已满");}break;
            //添加线路信息
            case 2:{n=scn.nextInt();
            if(route[n-1].station==null)System.out.println("所选对象为空");
            else route[n-1].Scn();}break;
            //修改线路信息
            case 3:{n=scn.nextInt();
            route[n-1].del();}break;
            //删除线路信息
            }break;
                }
            //线路管理
            case 3:{System.out.println("1.添加");
            System.out.println("2.修改");
            System.out.println("3.删除");
            m=scn.nextInt();
            switch(m)
            {//
            case 1:{for(i=0;i<=1;i++)
            if(employee[i].name==null) break;
            if(i!=2) employee[i].Scn();
            else System.out.println("信息已满");}break;
            //添加员工信息
            case 2:{n=scn.nextInt();
            if(employee[n-1].name==null)System.out.println("所选对象为空");
            else employee[n-1].Scn();}break;
            //修改员工信息
            case 3:{n=scn.nextInt();
            employee[n-1].del();}break;
            //删除员工信息
            }
                break;}
            //员工管理
            case 4:{
                System.out.println("1.添加");
                System.out.println("2.修改");
                System.out.println("3.删除");
                m=scn.nextInt();
                switch(m)
                {//
                case 1:{for(i=0;i<=1;i++)
                if(passenger[i].name==null) break;
                if(i!=2) passenger[i].Scn();
                else System.out.println("信息已满");}break;
                //添加乘客信息
                case 2:{n=scn.nextInt();
                if(passenger[n-1].name==null)System.out.println("所选对象为空");
                else passenger[n-1].Scn();}break;
                //修改乘客信息
                case 3:{n=scn.nextInt();
                passenger[n-1].del();}break;
                //删除乘客信息
                }
                    break;
            }
            //乘客管理
            case 5:{
                for(i=0;i<=1;i++)
                {System.out.println("线路"+(i+1));
                    bus[i].Prn();
                    route[i].Prn();
                    employee[i].Prn();
                    passenger[i].Prn();
                }
            }
        }
            }
    }
}
class Bus{Scanner scn=new Scanner(System.in);
String type;
int number;
int route;
int capacity;
void Scn() {System.out.println("请依次输入车辆的线路类型,车号,线路号,载客量,以回车分隔");
    type=scn.nextLine();
    number=scn.nextInt();
    route=scn.nextInt();
    capacity=scn.nextInt();}
void Prn(){System.out.println("线路类型:"+type);
    System.out.println("车号:"+number);
    System.out.println("线路号:"+route);
    System.out.println("载客量:"+capacity);}
    void del()
    {type=null;number=0;route=0;capacity=0;}
}
class Route{
Scanner scn=new Scanner(System.in);
int StopNo;
String station;
String destination;
String RunTime;
void Scn() {System.out.println("请依次输入线路的站数,始发站,终点站,运行时间,以回车分隔");
    StopNo=scn.nextInt();
    scn.nextLine();
    station=scn.nextLine();
    destination=scn.nextLine();
    RunTime=scn.nextLine();}
void Prn(){
    System.out.println("站数:"+StopNo);
    System.out.println("始发站:"+station);
    System.out.println("终点站:"+destination);
    System.out.println("运行时间:"+RunTime);   
}
    void del()
    {StopNo=0;station=null;destination=null;RunTime=null;}
    public class Route extends Local{
        
    }
    public class Route extends Distance{
        
    }
}
class Employee{
Scanner scn=new Scanner(System.in);
String name;
int ID;
int age;
double salary;
String department;
void Scn() {System.out.println("请依次输入员工的姓名,工号,年龄,工资,部门,以回车分隔");
    name=scn.nextLine();
    ID=scn.nextInt();
    age=scn.nextInt();
    salary=scn.nextDouble();
    scn.nextLine();
    department=scn.nextLine();}
void Prn(){
    System.out.println("姓名:"+name);
    System.out.println("工号:"+ID);
    System.out.println("年龄:"+age);
    System.out.println("工资:"+salary);
    System.out.println("部门:"+department);
}
void del()
{name=null;ID=0;age=0;salary=0;department=null;}
public class Employee extends manager{
    String position,duty,manage department;
}
}
class Passenger{
Scanner scn=new Scanner(System.in);
String name;
String sex;
int age;
String message;
void Scn() {System.out.println("请依次输入乘客的姓名,性别,年龄,信息,以回车分隔");
    name=scn.nextLine();
    sex=scn.nextLine();
    age=scn.nextInt();
    scn.nextLine();
    message=scn.nextLine();}
void Prn(){
    System.out.println("姓名:"+name);
    System.out.println("性别:"+sex);
    System.out.println("年龄:"+age);
    System.out.println("信息:"+message);
}
    void del()
    {name=null;sex=null;age=0;message=null;}
}
要求: (2)   对Employee模块进行扩展:派生manager类,manager具有职务、管理部门、职责等特性。
 (3)  对Route线路管理模块进行扩展:Route类派生出一个Local类和LongDistance类,Local类负责市内或近郊线路;LongDistance类负责长途客运。自行设计两个子类属性和相关操作。这两个类里都有一个Bus类对象作为数据成员,用于记录运行于这两种公交线路的所有Bus信息。
(4)  定义测试类以继承的方式实现以上模块。   
2018-05-08 11:02
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:22
帖 子:378
专家分:1655
注 册:2018-2-6
  得分:15 
程序代码:
import java.util.Scanner;

/**
 *
@author niyite
 *
 
*/
/**
 *
 * 要求: (2) 对Employee模块进行扩展:派生manager类,manager具有职务、管理部门、职责等特性。
 * (3)对Route线路管理模块进行扩展:Route类派生出一个Local类和LongDistance类,Local类负责市内或近郊线路;
 * LongDistance类负责长途客运。自行设计两个子类属性和相关操作。这两个类里都有一个Bus类对象作为数据成员,
 * 用于记录运行于这两种公交线路的所有Bus信息。 (4) 定义测试类以继承的方式实现以上模块。
 *
 
*/
public class Test {
    public static void main(String args[]) {
        Scanner scn = new Scanner(System.in);
        System.out.println("请选择功能");
        int t = 6, i, m, n;
        //
        Bus bus[] = new Bus[2];
        Route route[] = new Route[2];
        Employee employee[] = new Employee[2];
        Passenger passenger[] = new Passenger[2];
        //
        for (i = 0; i <= 1; i++) {
            bus[i] = new Bus();
            route[i] = new Route();
            employee[i] = new Employee();
            passenger[i] = new Passenger();
        }
        //
        while (t != 0) {
            System.out.println("1.车辆管理");
            System.out.println("2.线路管理");
            System.out.println("3.员工管理");
            System.out.println("4.乘客管理");
            System.out.println("5.输出信息");
            t = scn.nextInt();
            //
            switch (t) {
            //
            case 1: {
                System.out.println("1.添加");
                System.out.println("2.修改");
                System.out.println("3.删除");
                m = scn.nextInt();
                switch (m) {//
                case 1: {
                    for (i = 0; i <= 1; i++)
                        if (bus[i].type == null)
                            break;
                    if (i != 2)
                        bus[i].Scn();
                    else
                        System.out.println("信息已满");
                }
                    break;
                // 添加车辆信息
                case 2: {
                    n = scn.nextInt();
                    if (bus[n - 1].type == null)
                        System.out.println("所选对象为空");
                    else
                        bus[n - 1].Scn();
                }
                    break;
                // 修改车辆信息
                case 3: {
                    n = scn.nextInt();
                    bus[n - 1].del();
                }
                    break;
                }
                // 删除车辆信息
            }
                break;
            // 车辆管理
            case 2: {
                System.out.println("1.添加");
                System.out.println("2.修改");
                System.out.println("3.删除");
                m = scn.nextInt();
                switch (m) {//
                case 1: {
                    for (i = 0; i <= 1; i++)
                        if (route[i].station == null)
                            break;
                    if (i != 2)
                        route[i].Scn();
                    else
                        System.out.println("信息已满");
                }
                    break;
                // 添加线路信息
                case 2: {
                    n = scn.nextInt();
                    if (route[n - 1].station == null)
                        System.out.println("所选对象为空");
                    else
                        route[n - 1].Scn();
                }
                    break;
                // 修改线路信息
                case 3: {
                    n = scn.nextInt();
                    route[n - 1].del();
                }
                    break;
                // 删除线路信息
                }
                break;
            }
            // 线路管理
            case 3: {
                System.out.println("1.添加");
                System.out.println("2.修改");
                System.out.println("3.删除");
                m = scn.nextInt();
                switch (m) {//
                case 1: {
                    for (i = 0; i <= 1; i++)
                        if (employee[i].name == null)
                            break;
                    if (i != 2)
                        employee[i].Scn();
                    else
                        System.out.println("信息已满");
                }
                    break;
                // 添加员工信息
                case 2: {
                    n = scn.nextInt();
                    if (employee[n - 1].name == null)
                        System.out.println("所选对象为空");
                    else
                        employee[n - 1].Scn();
                }
                    break;
                // 修改员工信息
                case 3: {
                    n = scn.nextInt();
                    employee[n - 1].del();
                }
                    break;
                // 删除员工信息
                }
                break;
            }
            // 员工管理
            case 4: {
                System.out.println("1.添加");
                System.out.println("2.修改");
                System.out.println("3.删除");
                m = scn.nextInt();
                switch (m) {//
                case 1: {
                    for (i = 0; i <= 1; i++)
                        if (passenger[i].name == null)
                            break;
                    if (i != 2)
                        passenger[i].Scn();
                    else
                        System.out.println("信息已满");
                }
                    break;
                // 添加乘客信息
                case 2: {
                    n = scn.nextInt();
                    if (passenger[n - 1].name == null)
                        System.out.println("所选对象为空");
                    else
                        passenger[n - 1].Scn();
                }
                    break;
                // 修改乘客信息
                case 3: {
                    n = scn.nextInt();
                    passenger[n - 1].del();
                }
                    break;
                // 删除乘客信息
                }
                break;
            }
            // 乘客管理
            case 5: {
                for (i = 0; i <= 1; i++) {
                    System.out.println("线路" + (i + 1));
                    bus[i].Prn();
                    route[i].Prn();
                    employee[i].Prn();
                    passenger[i].Prn();
                }
            }

            }
        }
    }
}

class Bus {
    Scanner scn = new Scanner(System.in);
    String type;
    int number;
    int route;
    int capacity;

    void Scn() {
        System.out.println("请依次输入车辆的线路类型,车号,线路号,载客量,以回车分隔");
        type = scn.nextLine();
        number = scn.nextInt();
        route = scn.nextInt();
        capacity = scn.nextInt();
    }

    void Prn() {
        System.out.println("线路类型:" + type);
        System.out.println("车号:" + number);
        System.out.println("线路号:" + route);
        System.out.println("载客量:" + capacity);
    }

    void del() {
        type = null;
        number = 0;
        route = 0;
        capacity = 0;
    }
}

class Local extends Route {

}

class Distance extends Route {

}

class Route {
    Scanner scn = new Scanner(System.in);
    int StopNo;
    String station;
    String destination;
    String RunTime;

    void Scn() {
        System.out.println("请依次输入线路的站数,始发站,终点站,运行时间,以回车分隔");
        StopNo = scn.nextInt();
        scn.nextLine();
        station = scn.nextLine();
        destination = scn.nextLine();
        RunTime = scn.nextLine();
    }

    void Prn() {
        System.out.println("站数:" + StopNo);
        System.out.println("始发站:" + station);
        System.out.println("终点站:" + destination);
        System.out.println("运行时间:" + RunTime);
    }

    void del() {
        StopNo = 0;
        station = null;
        destination = null;
        RunTime = null;
    }

}

class Employee {
    Scanner scn = new Scanner(System.in);
    String name;
    int ID;
    int age;
    double salary;
    String department;

    void Scn() {
        System.out.println("请依次输入员工的姓名,工号,年龄,工资,部门,以回车分隔");
        name = scn.nextLine();
        ID = scn.nextInt();
        age = scn.nextInt();
        salary = scn.nextDouble();
        scn.nextLine();
        department = scn.nextLine();
    }

    void Prn() {
        System.out.println("姓名:" + name);
        System.out.println("工号:" + ID);
        System.out.println("年龄:" + age);
        System.out.println("工资:" + salary);
        System.out.println("部门:" + department);
    }

    void del() {
        name = null;
        ID = 0;
        age = 0;
        salary = 0;
        department = null;
    }

}

class Manager extends Employee {
    String position, duty, manage;
}

class Passenger {
    Scanner scn = new Scanner(System.in);
    String name;
    String sex;
    int age;
    String message;

    void Scn() {
        System.out.println("请依次输入乘客的姓名,性别,年龄,信息,以回车分隔");
        name = scn.nextLine();
        sex = scn.nextLine();
        age = scn.nextInt();
        message = scn.nextLine();
    }

    void Prn() {
        System.out.println("姓名:" + name);
        System.out.println("性别:" + sex);
        System.out.println("年龄:" + age);
        System.out.println("信息:" + message);
    }

    void del() {
        name = null;
        sex = null;
        age = 0;
        message = null;
    }
}

你debug运行一下,太长了,没时间给你细看

假如人生没有梦想,和咸鱼有什么区别!
2018-05-09 14:13
脚丫很香
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2018-5-8
  得分:0 
回复 2楼 疯狂的小a
老哥我问一下那个Route的两个子类为什么是空的啊?要求不是让自己设计属性吗?……虽然我不会设计
2018-05-11 20:07
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:22
帖 子:378
专家分:1655
注 册:2018-2-6
  得分:0 
因为Local和Distance都是Route的子类,子类继承父类属性。如果你想子类有特殊的属性,可以加上!

假如人生没有梦想,和咸鱼有什么区别!
2018-05-11 23:20
脚丫很香
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2018-5-8
  得分:0 
回复 4楼 疯狂的小a
那两个类都得以bus为对象是啥意思,明天要交作业,我运行出来为啥没有变化?嘤嘤嘤
2018-05-14 21:41
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:22
帖 子:378
专家分:1655
注 册:2018-2-6
  得分:0 
就是说Local和Distance里面都有成员属性Bus
你可以
class Local extends Route {
    public Bus LocalBus;
}

class Distance extends Route {
    public Bus DistanceBus;
}

假如人生没有梦想,和咸鱼有什么区别!
2018-05-14 22:11







关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.056165 second(s), 8 queries.
Copyright©2004-2018, BCCN.NET, All Rights Reserved