java_day7练习作业(面向对象下-1)

976-沈同学

发表文章数:64

首页 » Java » 正文

练习

练习1(继承)

1.1.训练描述:
一、分析以下需求,并用代码实现:
1.定义Person类
属性:姓名name、年龄age;
		方法:空参构造方法、满参构造方法、setXxx()、getXxx()、工作work。
		 
2.根据人类,派生一个老师类Teacher 
		重写工作方法(老师的工作是要好好讲课)。
			
3.根据人类,派生一个学生类Student 
		重写工作方法(学生的工作是要好好学习)。
		
4.编写测试类分别对上述2类具体人物进行测试

5.要求运行结果:
					学生的工作要好好学习
					老师的工作要好好讲课
package com.scy;	//基类 Person 

public class Person {
    private String name;
    private int age;
    public Person(){}
    public Person(String name, int age){
        this.name = name;
        this.age = age;
    }
    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 void work(){
        System.out.println("人是要工作的");
    }
}
package com.scy;		//继承类 Student 

public class Student extends Person {
    public Student(){}
    public Student(String name,int age){
        super(name,age);
    }
    @Override
    public void work(){
        System.out.println("学生的工作是要好好学习");
    }
}
package com.scy;	//继承类 Teacher 

public class Teacher extends Person {
    public Teacher(){}
    public Teacher(String name,int age){
        super(name,age);
    }
    @Override
    public void work(){
        System.out.println("老师的工作是要好好讲课");
    }
}
package com.scy;	//测试类

public class MainApp {
    public static void main(String[] args) {
        Person p = new Student("wang",25);
        p.work();
        p = new Teacher();
        p.setName("liu");
        p.setName("30");
        p.work();
    }
}

练习2(继承/方法重写)

一、分析以下需求,要求用代码实现:
1.定义手机类
		方法:打电话(手机的功能是给某人打电话)
		
2.根据手机类,派生一个新手机类
		重写打电话功能(新功能可以开启视频电话,同时要包含老功能)
		
3.编写测试类分别对上述2类手机进行测试
4.要求运行结果:
				给林青霞打电话
				----------------------
				开启视频功能
				给林青霞打电话
package com.scy;	// Phone 父类

public class Phone {
    private String name;
    public Phone(){}
    public Phone(String name){
        this.name = name;
    }
    public void call(String name){
        System.out.println("给"+name+"打电话");
    }
}
package com.scy;	// NewPhone 子类

public class NewPhone extends Phone {
    public NewPhone(){}
    public NewPhone(String name){
        super(name);
    }
    @Override
    public void call(String name){
        System.out.println("开启视频功能");
        super.call(name);
    }
}
package com.scy;	//测试类

public class MainApp {
    public static void main(String[] args) {
       Phone p = new Phone("林青霞");
       p.call(p.getName());
       System.out.println("-----------------");
       Phone p1 = new NewPhone("林青霞");
       p1.call(p1.getName());
    }
}

练习3(多态)

一、分析以下需求,并用代码实现:
1.定义Animal动物类
		方法:吃东西eat。 
2.根据动物类,派生一个猫类Cat 
		重写吃东西方法(猫吃鱼)。	
3.根据动物类,派生一个狗类Dog 
		重写吃东西方法(狗吃骨头)。
		狗还有一个特有方法,狗看门
4.根据动物类,派生一个猪类Pig 
		重写吃东西方法(猪吃白菜)。
5.根据操作动物类AnimalOperator
方法:调用动物的吃方法 
6.编写测试类分别对上述4类事物AnimalOperator、Cat、Dog、Pig创建对象,调用吃东西功能,进行测试
7.要求运行结果:
	猫吃鱼
	狗吃骨头
	猪吃白菜
package com.scy;  // 父类animal

public class Animal {
    public void eat(){
        System.out.println("吃东西");
    }
}
package com.scy;	//子类cat

public class Cat extends Animal{
    @Override
    public void eat(){
        System.out.println("猫吃鱼");
    }
}
package com.scy; //子类dog

public class Dog extends Animal{
    public void eat(){
        System.out.println("狗吃骨头");
    }
    public void lookDoor(){
        System.out.println("狗看门");
    }
}
package com.scy;	// 子类pig

public class Pig extends Animal {
    public void eat(){
        System.out.println("猪吃白菜");
    }
}
package com.scy;	// 操作动物类
public class AnimalOperate {
    public void useAnimal(Animal a ){ //Animal a = new Cat();
        a.eat();
    }
}
package com.scy;	// 测试类

public class MainApp {
    public static void main(String[] args) {
        Cat c = new Cat();
        Dog d = new Dog();
        Pig p = new Pig();
        AnimalOperate ao = new AnimalOperate();
        ao.useAnimal(c);
        ao.useAnimal(d);
        ao.useAnimal(p);
    }
}

练习4(多态)

一、分析以下需求,并用代码实现:
1. 定义Person类
		属性:
			姓名name、性别gender、年龄age、国籍nationality;
		方法:吃饭eat、睡觉sleep,工作work。
	2.根据人类,派生一个学生类Student
		增加属性:
			学校school、学号stuNumber;
		重写工作方法(学生的工作是学习)。	
	3.根据人类,派生一个工人类Worker
		增加属性:
			单位unit、工龄workAge;
		重写工作方法(工人的工作是盖房子)。
	4.根据学生类,派生一个学生干部类 StudentLeader
		增加属性:
			职务job;
		增加方法:开会meeting。
	5.编写测试类分别对上述3类具体人物进行测试。
	6.要求运行结果:
		学生需要学习!
		工人的工作是盖房子!
		学生干部喜欢开会!
package com.scy;  //父类 person

public class Person {
    private String name;
    private String gender;
    private int age;
    private String nationality;

    public Person(){}
    public Person(String name,String gender,int age,String nationality){
        this.name=name;
        this.gender=gender;
        this.age = age;
        this.nationality = nationality;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
    public String getNationality() {
        return nationality;
    }
    public void setNationality(String nationality) {
        this.nationality = nationality;
    }

    public void eat(){
        System.out.println("吃饭");
    }
    public void sleep(){
        System.out.println("睡觉");
    }
    public void work(){
        System.out.println("工作");
    }

}
package com.scy;	// 子类 Student

public class Student extends Person {
    private String school;
    private int stdNumber;

    public Student(){}
    public Student(String name,String gender,int age,String nationality){
        super(name,gender,age,nationality);
    }

    public Student(String school, int stdNumber){
        this.school = school;
        this.stdNumber = stdNumber;
    }


    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }

    public int getStdNumber() {
        return stdNumber;
    }

    public void setStdNumber(int stdNumber) {
        this.stdNumber = stdNumber;
    }

    @Override
    public void work(){
        System.out.println("学生需要学习!");
    }
}
package com.scy;	//子类Worker

public class Worker extends Person {
    private String unit;
    private int workAge;
    public Worker(){}
    public Worker(String name,String gender,int age,String nationality){
        super(name,gender,age,nationality);
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    public int getWorkAge() {
        return workAge;
    }

    public void setWorkAge(int workAge) {
        this.workAge = workAge;
    }

    @Override
    public void work(){
        System.out.println("工人的工作是盖房子!");
    }

}
package com.scy;	// 学生的子类StudentLeader

public class StudentLeader extends Student {
    private String job;

    public StudentLeader(){}

    public StudentLeader(String school, int stdNumber){
        super(school,stdNumber);
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public void meeting(){
        System.out.println("学生干部喜欢开会!");
    }

}
package com.scy;	// Person操作类

public class PersonOperate {
    public void PersonWork(Person p){
        p.work();
    }
}
package com.scy;	// 测试类

public class MainApp {
    public static void main(String[] args) {
       PersonOperate po = new PersonOperate();
       Person p = new Student("wang","男",20,"中国");
       po.PersonWork(p);
       Person p1 = new Worker("liu","男",40,"韩国");
       po.PersonWork(p1);
       Student p3 = new StudentLeader("南京大学",10086);
       StudentLeader p4 = (StudentLeader) p3;
       p4.meeting();

    }
}

练习5(多态)

一、分析以下需求,并用代码实现
1.定义项目经理类 
			属性:
				姓名 工号 工资 奖金
			行为:
				工作work
2.定义程序员类
			属性:
				姓名 工号 工资
			行为:
				工作work
3.要求:向上抽取一个父类,让这两个类都继承这个父类,共有的属性写在父类中,子类重写父类中的方法
4.编写测试类:完成这两个类的测试
package com.scy;  // 父类 Person

public class Person {
    private String name;
    private int work_num;
    private int salary;

    public Person(){}

    public Person(String name, int work_num, int salary){
        this.name = name;
        this.work_num = work_num;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getWork_num() {
        return work_num;
    }

    public void setWork_num(int work_num) {
        this.work_num = work_num;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    public void work(){
        System.out.println("人需要工作");
    }

}
package com.scy;	//子类Manager

public class Manager extends Person{
    private int bonus;

    public  Manager(){}

    public Manager(String name,int work_num,int salary){
        super(name,work_num,salary);
    }

    public int getBonus() {
        return bonus;
    }

    public void setBonus(int bonus) {
        this.bonus = bonus;
    }

    @Override
    public void work(){
        System.out.println("项目经理的工作是负责项目");
    }

}
package com.scy;	//子类Programmer

public class Programmer extends Person {
    public Programmer(){}
    public Programmer(String name,int work_num,int salary){
        super(name,work_num,salary);
    }

    @Override
    public void work(){
        System.out.println("程序员的工作是写代码");
    }
}
package com.scy;	// person操作类

public class PersonOperate {
    public void PersonWork(Person p){
        p.work();
    }
}
package com.scy;	//测试类

public class MainApp {
    public static void main(String[] args) {
       PersonOperate po = new PersonOperate();
       Person p1 = new Manager("wang",101,5000);
       po.PersonWork(p1);
       Person p2 = new Programmer("li",005,10000);
       po.PersonWork(p2);

    }
}

练习6(多态)

一、分析以下需求,并用代码实现
1.已知学生类和老师类如下:
			属性:
				姓名,年龄
			行为:
				吃饭
			老师有特有的方法:
				讲课
			学生有特有的方法:
				学习

2.要求:向上抽取一个父类,让这两个类都继承这个父类,共有的属性写在父类中,子类重写父类中的方法
3.编写测试类:完成这两个类的测试
package com.scy;	// 父类Person

public class Person {
    private String name;
    private int age;

    public Person(){}

    public Person(String name, int age){
        this.name= name;
        this.age = age;
    }

    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;
    }
    
}
package com.scy;	// 子类Student

public class Student extends Person {
    public Student(){}
    public Student(String name,int age){
        super(name,age);
    }
    public void workStudent(){
        System.out.println("学生爱学习");
    }
}
package com.scy;	// 子类Teacher

public class Teacher extends Person {
    public Teacher(){}
    public Teacher(String name,int age){
        super(name,age);
    }
    public void workTeacher(){
        System.out.println("老师爱讲课");
    }
}
package com.scy;	// 测试类

public class MainApp {
    public static void main(String[] args) {
       Person p1 = new Student("wang",15);
       Student std = (Student) p1;
       std.workStudent();
       Person p2 = new Teacher("liu",30);
       Teacher t = (Teacher) p2;
       t.workTeacher();
    }
}

练习7(多态)

一、分析以下需求,并用代码实现:
	1.定义员工类:
		属性:
			工号,姓名
		行为:
			工作方法(用简单的输出语句模拟)
	2.定义经理类:
		属性:
			工号,姓名,奖金
		行为:
			工作方法(管理员工,简单输出语句模拟)
	3.定义服务员类:
		属性:
			工号,姓名
		行为:
			工作方法(为客户服务,简单输出语句模拟)
	4.定义测试类:
		分别创建经理类对象和服务员类对象
		调用各自的工作方法
		要求:
			分析各个事物之间的继承关系,抽取出相关的类
package com.scy;

public class Staff {
    private int work_num;
    private String name;

    public Staff(){}
    public Staff(int work_num, String name){
        this.work_num = work_num;
        this.name = name;
    }

    public void method(){
        System.out.print("员工应该做的事");
    }
}
package com.scy;

public class Manager extends Staff{
    private int bonus;

    public Manager(){}

    public Manager(int work_num,String name){
        super(work_num,name);
    }

    public int getBonus() {
        return bonus;
    }

    public void setBonus(int bonus) {
        this.bonus = bonus;
    }

    @Override
    public void method(){
        super.method();
        System.out.print(",管理员工");
        System.out.println();
    }

}
package com.scy;

public class Waiter extends Staff {


    public Waiter(){}

    public Waiter(int work_num,String name){
        super(work_num,name);
    }

    @Override
    public void method(){
        super.method();
        System.out.print(",为客户服务");
        System.out.println();
    }
}
package com.scy;

public class MainApp {
    public static void main(String[] args) {
      Staff s1 = new Manager(001,"wang");
      s1.method();
      Staff s2 = new Waiter(002,"liu");
      s2.method();
    }
}

拜师教育学员文章:作者:976-沈同学, 转载或复制请以 超链接形式 并注明出处 拜师资源博客
原文地址:《java_day7练习作业(面向对象下-1)》 发布于2020-03-09

分享到:
赞(0) 打赏

评论 抢沙发

评论前必须登录!

  注册



长按图片转发给朋友

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

Vieu3.3主题
专业打造轻量级个人企业风格博客主题!专注于前端开发,全站响应式布局自适应模板。

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

Q Q 登 录
微 博 登 录