抽象类例题,

葵花宝典(更好的理解抽象类)

package lesson01;

public class demo01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//葵花宝典(更好的理解抽象类)
		
		葵花宝典 yue = new 岳不群();
		yue.自宫();

	}

}
abstract class 葵花宝典{
	public void 自宫() {
		
	}
}
class 岳不群 extends 葵花宝典{
	public void 自宫() {
		System.out.println("用剑。。");
	}
}
class 林平之 extends 葵花宝典{
	public void 自宫() {
		System.out.println("用刀。。。");
	}
}






抽象类练习员工案例

package lesson03;

public class demo01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/*面试题
		 * 使用抽象类练习员工的案例
		 * 程序员包括3个属性:姓名,工号以及工资
		 * 经理除了包含程序员的属性外,另外还有一个奖金属性
		 * 请使用继承的思想设计出程序和经理类,
		 * 要求类中提供必要的方法进行属性访问
		 */
		Programmer pro1 = new Programmer("小刘",0001,190900);
		pro1.say();
		System.out.println("========");
		Mannager pro2 = new Mannager("小贾",00033,143443,"134324");
		pro2.say();

	}

}
abstract class Employee{
	String name;
	int id;
	double salary;
	public abstract void say();
	
	public Employee(String name, int id, double salary) {
		super();//提供了有参的构造方法系统就不提供午餐的构造方法了,此时就要你自己写出来
		this.name = name;
		this.id = id;
		this.salary = salary;
	}
	public Employee() {
		super();//无惨的构造方法
		// TODO Auto-generated constructor stub
	}
	

}

class Programmer extends Employee{

	
	@Override
	public void say() {
		// TODO Auto-generated method stub
		System.out.println("我叫"+name);
		System.out.println("我的工号是"+id);
		System.out.println("我的月工资"+salary);
	}

	public Programmer() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Programmer(String name, int id, double salary) {
		super(name, id, salary);
		// TODO Auto-generated constructor stub
	}
	

	
	
	
	
}
class Mannager extends Employee{
	String bonus;

	@Override
	public void say() {
		// TODO Auto-generated method stub
		System.out.println("我叫"+name);
		System.out.println("我的工号是"+id);
		System.out.println("我的月工资"+salary);
		System.out.println("我的奖金"+bonus);
		
	}

	public Mannager() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Mannager(String name, int id, double salary,String bonus) {
		super(name, id, salary);
		this.bonus=bonus;
		// TODO Auto-generated constructor stub
	}
	
	
}










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include<iostream> using namespace std; class C { public: float card_fee; virtual void real_fee() = 0; virtual void show_the_real_fee() = 0; }; class student : public C { public: student(float fee) { card_fee = fee; } void real_fee(){ card_fee *= 0.5; }//计算学生的实际费用 void show_the_real_fee(){ cout << "学生实际的费用是:" << card_fee<<"元"<<endl; } }; class older : public C { public: older(float fee){ card_fee = fee; } void real_fee(){ card_fee *= 0.6; }//计算老人的实际费用 void show_the_real_fee(){ cout << "老人实际的费用是:" << card_fee << "元" << endl; } }; class normal : public C { public: normal(float fee){ card_fee = fee; } void real_fee(){ card_fee *= 0.95; }//计算一般的实际费用 void show_the_real_fee(){ cout << "一般市民实际的费用是:" << card_fee << "元" << endl; } }; void main() { cout << "请输入公交费原价:"; float fee; cin >> fee; cout <<"公交费原价为"<<fee<<"元"<< endl; cout << "请输入刷人群性质" << endl << "学生:请输 1;" << endl << "老人:请输 2;" << endl << "普通:请输 3;" << endl; int man; cin >> man;//2.定义了一个变量,用于了解用户刷的的种,以便进行相应的计算 C *p_card;//3.定义了一个指针,为了方便对子的操作(即:该指针指向"谁"时,用这个指针调用的函数就是"谁"的函数,因为子的函数的名子都一样,函数里的内容不一样,调用不同子的函数后,计算的结果就不同) switch (man)//4.根据用户的输入,开启相应的功能 { case 1: {student stu_card(fee); p_card = &stu_card; p_card->real_fee(); p_card->show_the_real_fee();}break; case 2: {older old_card(fee); p_card = &old_card; p_card->real_fee(); p_card->show_the_real_fee();}break; case 3: {normal normal_card(fee); p_card = &normal_card; p_card->real_fee(); p_card->show_the_real_fee();}break; default:cout <<"输入错误!"<< endl;//5.如果用户输入的不是1,2,3,而是其他字符,则报错 } getchar();getchar(); }
假设我们要设计一个动物园的程序,其有多种动物,每种动物都有自己的特性和行为。这时候,我们可以使用面向对象的设计思想来实现这个程序。 首先,我们可以定义一个抽象类 Animal,它包含动物的基本属性和行为,例如: ```java public abstract class Animal { protected String name; protected int age; public Animal(String name, int age) { this.name = name; this.age = age; } public abstract void eat(); public abstract void sleep(); public abstract void makeSound(); } ``` 在这个抽象类,我们定义了动物的基本属性 name 和 age,以及三个抽象方法 eat、sleep 和 makeSound,表示动物的基本行为。由于每种动物的具体实现不同,因此这些方法只是声明而不实现,留给子去具体实现。 接下来,我们可以定义几个具体的动物子,例如: ```java public class Lion extends Animal { public Lion(String name, int age) { super(name, age); } @Override public void eat() { System.out.println("Lion is eating meat."); } @Override public void sleep() { System.out.println("Lion is sleeping."); } @Override public void makeSound() { System.out.println("Roar!"); } } public class Elephant extends Animal { public Elephant(String name, int age) { super(name, age); } @Override public void eat() { System.out.println("Elephant is eating grass."); } @Override public void sleep() { System.out.println("Elephant is sleeping."); } @Override public void makeSound() { System.out.println("Trumpet!"); } } ``` 在这些子,我们重写了父的抽象方法,并实现了具体的行为。例如,Lion 的 eat 方法表示狮子吃肉,而 Elephant 的 eat 方法表示大象吃草。 最后,我们可以在主程序创建一个动物园对象,添加各种不同的动物,例如: ```java public class Zoo { private List<Animal> animals; public Zoo() { animals = new ArrayList<>(); } public void addAnimal(Animal animal) { animals.add(animal); } public void showAnimals() { for (Animal animal : animals) { System.out.println(animal.name + " is " + animal.age + " years old."); animal.eat(); animal.sleep(); animal.makeSound(); System.out.println(); } } } public class Main { public static void main(String[] args) { Zoo zoo = new Zoo(); zoo.addAnimal(new Lion("Simba", 3)); zoo.addAnimal(new Elephant("Dumbo", 5)); zoo.showAnimals(); } } ``` 在主程序,我们创建了一个动物园对象 zoo,添加了一只狮子和一头大象,然后展示了它们的基本信息和行为。运行程序,可以看到如下输出: ``` Simba is 3 years old. Lion is eating meat. Lion is sleeping. Roar! Dumbo is 5 years old. Elephant is eating grass. Elephant is sleeping. Trumpet! ``` 通过这个例子,我们可以看到抽象类和接口的使用。Animal 作为抽象类,定义了动物的基本属性和行为,并声明了一些抽象方法,留给子去实现。而 Lion 和 Elephant 作为具体的子,继承了 Animal ,并实现了它的抽象方法,以便具体实现狮子和大象的行为。最后,在 Zoo ,我们使用 Animal 的对象来表示动物,从而可以添加不同种的动物并展示它们的基本信息和行为。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值