1接口练习
请定义“员工(类)”:
? 属性:姓名、性别、年龄(全部私有) ? 行为:工作(抽象) ? 无参、全参构造方法 ? get/set方法
package com.csdn.day03.one;
public abstract class Employee {
private String name;
private String sex;
private int age;
public Employee() {
}
public Employee(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public abstract void work();
}
1.2. 请定义“绘画(接口)” ? 抽象方法:绘画
package com.csdn.day03.one;
public interface Drawing {
void drawing();
}
1.3. 请定义“Java讲师类”继承自“员工类”
package com.csdn.day03.one;
public class JavaTeacher extends Employee{
@Override
public void work() {
System.out.println("讲java");
}
}
1.4. 请定义”UI讲师类”,继承自“员工类”,并实现“绘画”接口。
package com.csdn.day03.one;
public class UiTeacher extends Employee implements Drawing{
@Override
public void drawing() {
System.out.println("我会画画");
}
@Override
public void work() {
System.out.println("讲Ui");
}
}
1.5.测试类
package com.csdn.day03.one;
public class UiTeacher extends Employee implements Drawing{
@Override
public void drawing() {
System.out.println("我会画画");
}
@Override
public void work() {
System.out.println("讲Ui");
}
}
2.接口练习
一个机动车管理系统中有以下类的设计: 机动车类: |–汽车类: |–奔驰车 |–宝马车 |–摩托车类: |–雅马哈摩托 |–宝马摩托 所有的车都有如下信息: ? 属性:品牌、价格 ? 行为:运行 现在要求为所有“宝马车系”的汽车增加一个GPS功能。 请设计出上述的类结构,并用代码实现。
2.1机动车类:
package com.csdn.day03.two;
public abstract class MotorVehicles {
private String brand;
private int price;
public MotorVehicles() {
}
public MotorVehicles(String brand, int price) {
this.brand = brand;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public abstract void run();
}
汽车类
package com.csdn.day03.two;
public abstract class Car extends MotorVehicles {
public Car() {
}
public Car(String brand, int price) {
super(brand, price);
}
}
奔驰车
package com.csdn.day03.two;
public class Benz extends Car{
@Override
public void run() {
System.out.println("奔驰汽车在飞驰");
}
}
宝马车
package com.csdn.day03.two;
public class BmwCar extends Car implements Gps{
@Override
public void run() {
System.out.println("宝马汽车在飞驰");
}
@Override
public void location() {
System.out.println("正在定位");
}
}
摩托车类
package com.csdn.day03.two;
public abstract class Motocross extends MotorVehicles{
public Motocross() {
}
public Motocross(String brand, int price) {
super(brand, price);
}
}
雅马哈摩托
package com.csdn.day03.two;
public class Yamaha extends MotorVehicles{
@Override
public void run() {
System.out.println("雅马哈在飞驰");
}
}
宝马摩托
package com.csdn.day03.two;
public class BmwM extends MotorVehicles implements Gps{
@Override
public void run() {
System.out.println("宝马摩托车在飞驰");
}
@Override
public void location() {
System.out.println("正在GPS定位");
}
}
GPS接口类
package com.csdn.day03.two;
public interface Gps {
void location() ;
}
3 多态练习
有以下类定义:
? 动物类: ? 属性:名称、年龄、性别 ? 行为:吃饭(非抽象) ? 猫类继承自动物 ? 行为:捉老鼠
动物类
package com.csdn.day03.three;
public class Animal {
private String type;
private int age;
private String sex;
public Animal() {
}
public Animal(String type, int age, String sex) {
this.type = type;
this.age = age;
this.sex = sex;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
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;
}
public void eat(){
System.out.println("动物吃饭");
}
}
猫类
package com.csdn.day03.three;
public class Cat extends Animal{
public void cMouse(){
System.out.println("捉老鼠");
}
}
测试类
package com.csdn.day03.three;
public class Test03 {
public static void main(String[] args) {
Animal a = new Cat();
a.eat();
Cat c = (Cat) a;
c.cMouse();
}
}
|