java抽象类练习题

package com.test;
/*
 * 2、定义一个抽象的"Role"类,有姓名,年龄,性别等成员变量
1)要求尽可能隐藏所有变量(能够私有就私有,能够保护就不要公有),
再通过GetXXX()和SetXXX()方法对各变量进行读写。具有一个抽象的play()方法,
该方法不返回任何值,同时至少定义两个构造方法。Role类中要体现出this的几种用法。
2)从Role类派生出一个"Employee"类,该类具有Role类的所有成员(构造方法除外),
并扩展salary成员变量,同时增加一个静态成员变量“职工编号(ID)”。
同样要有至少两个构造方法,要体现出this和super的几种用法,还要求覆盖play()方法,
并提供一个final sing()方法。
3)"Manager"类继承"Employee"类,有一个final成员变量"vehicle"
在main()方法中制造Manager和Employee对象,并测试这些对象的方法。
 * */

abstract class Role {
	private String name;
	private int age;
	private String sex;
	
	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;
	}
	
	public abstract void play ();
	
	public Role (){
		
	}
	public Role (String name, int age, String sex){
		this.name = name;
		this.age = age;
		this.sex = sex;
	}

}

class Employee extends Role {
	private double salary;
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	private static String ID;
	
	public static String getID() {
		return ID;
	}
	public static void setID(String iD) {
		ID = iD;
	}
	public Employee () {
		
	}
	public Employee (String name, int age, String sex, double salary, String ID) {
		super(name, age, sex);
		this.salary = salary;
		this.ID = ID;
	}

	@Override
	public void play() {
		// TODO Auto-generated method stub
		System.out.println("员工编号"+this.getID());
		
	}
	final void sing() {
		System.out.println("员工姓名"+this.getName()+"员工性别"+this.getSex()
				+"员工年龄"+this.getAge()+"员工工资"+this.getSalary());
		
	}
	
}

class Manager extends Employee {
	final String vehicle;
	
	public Manager (String name, int age, String sex, double salary, String ID, String vehicle) {
		super(name, age, sex, salary, ID);
		this.vehicle = "保时捷";
	}
}

public class Demo2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Employee e = new Employee("张三", 27, "男", 3000,"001");
		e.play();
		e.sing();
		Manager m = new Manager("李四", 29, "男", 4000, "002", "");
		m.play();
		m.sing();
		System.out.println("坐车"+m.vehicle);

	}

}

### 关于 Java 抽象类与接口的练习题 以下是几个针对抽象类和接口设计的经典练习题目及其代码实现: --- #### 题目 1: 设计一个动物系统 创建一个 `Animal` 抽象类,其中定义了一个抽象方法 `makeSound()` 和一个具体方法 `eat()`。再分别派生两个子类 `Dog` 和 `Cat` 实现各自的发声行为。 ```java // 定义抽象类 Animal abstract class Animal { // 抽象方法 makeSound() abstract void makeSound(); // 具体方法 eat() void eat() { System.out.println("Eating..."); } } // 子类 Dog class Dog extends Animal { @Override void makeSound() { System.out.println("Bark"); } } // 子类 Cat class Cat extends Animal { @Override void makeSound() { System.out.println("Meow"); } } public class Main { public static void main(String[] args) { Animal myDog = new Dog(); myDog.makeSound(); // 输出 Bark myDog.eat(); // 输出 Eating... Animal myCat = new Cat(); myCat.makeSound(); // 输出 Meow myCat.eat(); // 输出 Eating... } } ``` 上述代码展示了如何通过抽象类来约束子类的行为[^1]。 --- #### 题目 2: 使用接口模拟支付功能 创建一个名为 `Payment` 的接口,该接口包含一个方法 `pay(int amount)` 表示付款金额的功能。然后让不同的支付方式(如信用卡、支付宝)去实现这个接口。 ```java // 接口 Payment interface Payment { void pay(int amount); } // 类 CreditCard 实现 Payment 接口 class CreditCard implements Payment { @Override public void pay(int amount) { System.out.println("Paid $" + amount + " using Credit Card."); } } // 类 Alipay 实现 Payment 接口 class Alipay implements Payment { @Override public void pay(int amount) { System.out.println("Paid ¥" + amount + " using Alipay."); } } public class Main { public static void main(String[] args) { Payment creditCard = new CreditCard(); creditCard.pay(100); // 输出 Paid $100 using Credit Card. Payment alipay = new Alipay(); alipay.pay(50); // 输出 Paid ¥50 using Alipay. } } ``` 此例子说明了接口用于描述一组通用操作的能力[^3]。 --- #### 题目 3: 结合抽象类与接口的设计模式 假设有一个图形绘制程序,所有的形状都继承自同一个抽象基类 `Shape` 并且实现了绘图接口 `Drawable` 中的方法 `draw()` 来完成具体的绘画逻辑。 ```java // 抽象类 Shape abstract class Shape { protected String color; public Shape(String color) { this.color = color; } public abstract double getArea(); } // Drawable 接口 interface Drawable { void draw(); } // 圆形 Circle 继承 Shape 同时实现 Drawable class Circle extends Shape implements Drawable { private double radius; public Circle(double radius, String color) { super(color); this.radius = radius; } @Override public double getArea() { return Math.PI * radius * radius; } @Override public void draw() { System.out.println("Drawing a circle with color " + color + "."); } } // 正方形 Square 继承 Shape 同时实现 Drawable class Square extends Shape implements Drawable { private double sideLength; public Square(double sideLength, String color) { super(color); this.sideLength = sideLength; } @Override public double getArea() { return sideLength * sideLength; } @Override public void draw() { System.out.println("Drawing a square with color " + color + "."); } } public class Main { public static void main(String[] args) { Drawable shape1 = new Circle(5, "Red"); shape1.draw(); // Drawing a circle with color Red. System.out.println(shape1 instanceof Circle); Drawable shape2 = new Square(4, "Blue"); shape2.draw(); // Drawing a square with color Blue. System.out.println(shape2 instanceof Square); } } ``` 这段代码体现了抽象类提供共享属性而接口负责特定行为分离的原则[^2]。 --- #### 总结 以上三个实例覆盖了从简单到复杂的场景应用,帮助理解并掌握 Java抽象类与接口的概念以及它们之间的区别和联系。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值