(一)抽象类,接口,HashMap
1.定义抽象类Animal,定义两个行为吃eat()和移动move();
abstract class Animal {
public int weight;
public abstract void eat() ;
public abstract void move();
}
※ 抽象类不能被实例化,被继承后子类必须重写继承的方法
2.通过继承实现类猫Cat、类狗Dog、类鸟Bird、猎鹰Falcon;
2.1 eat():猫输出“吃鱼。”,狗输出“吃骨头。”,鸟输出“吃虫子。”,猎鹰输出“吃蛇。”
2.2 move(): 猫输出“跳。”,狗输出“跑。”,鸟输出“飞。”,猎鹰输出“飞很高。”
class Cat extends Animal{
public void eat() {System.out.println("吃鱼");super.weight+=5+(int)(Math.random()*10);}
public void move() {System.out.println("跳");}
}
class Dog extends Animal{
public void eat() {System.out.println("吃骨头");super.weight+=7+(int)(Math.random()*10);}
public void move() {System.out.println("跑");}
}
class Bird extends Animal{
public void eat() {System.out.println("吃虫子");super.weight+=2+(int)(Math.random()*10);}
public void move() {System.out.println("飞");}
}
class Falcon extends Animal{
public void eat() {System.out.println("吃蛇");super.weight+=110+(int)(Math.random()*10);}
public void move() {System.out.println("飞得很高");}
}
3.定义IPet接口,定义行为Play();
interface IPet{void play();}
※ 接口只能有 抽象方法 和 不可变常量 (final)
4.实现类PetDog,PetCat,PetRobot;
4.1 Play的行为PetDog 输出“叼飞盘”,PetCat,输出“撸撸猫”,PetRobot,输出“编程玩吧”
4.2 为类PetDog,PetCat添加名字属性Name, 以及get_name和set_name方法
class PetDog implements IPet{
private String Name;
public String get_Name() {return Name;}
public void set_Name(Scanner s) {
System.out.println("Please enter its name:");
Name=s.nextLine();
}
public void play() {System.out.println("叼飞盘");}
}
class PetCat implements IPet{
private String Name;
public String get_Name() {return Name;}
public void set_Name(Scanner s) {
System.out.println("Please enter its name:");
Name=s.nextLine();
}
public void play() {System.out.println("撸撸猫");}
}
class PetRobot implements IPet{
public void play() {System.out.println("编程玩吧");}
}
5.生成测试类Stage,在测试类中生成3只狗,2只猫,1只猎鹰,1只鸟,1只宠物狗,1只宠物猫,1只机器宠物;
5.1 为宠物猫和宠物狗取名
5.2生成list列表,用于存放生成的所有动物,通过迭代器遍历列表,让所有动物吃东西eat(),移动move()
5.3 为所有动物增加体重属性,每次吃东西后会增加体重,每个动物体重增加基于种类,且应该是随机的
5.4 按体重为动物排序
5.5 将所有宠物存储在哈希表中,根据用户输入宠物名称,从哈希表中找出相应对象交互play()
public class 实验 {
public static void main(String[]a) {
List<Animal> a1=new ArrayList<Animal>() ;
for(int i=0;i<2;i++)
{a1.add(new Cat());}
for(int i=0;i<3;i++)
{a1.add(new Dog());}
a1.add(new Bird()) ;
a1.add(new Falcon()) ;
List<IPet> p=new ArrayList<IPet>() ;
Scanner s=new Scanner(System.in);//Scanner关闭后无法再次打开
PetDog pdog=new PetDog();
pdog.set_Name(s);
p.add(pdog);
PetCat pcat=new PetCat();
pcat.set_Name(s);
p.add(pcat);
s.close();
for(Animal ia:a1)
{
ia.eat();ia.move();
}
for(IPet ip:p)
{
ip.play();
}
Collections.sort(a1, new Comparator<Animal>(){
public int compare(Animal o1, Animal o2) {
//按照金额大小进行降序排列
if(o1.weight < o2.weight){
return 1;
}
else if(o1.weight == o2.weight){
return 0;
}
else return -1;
}
});
for(Animal ia:a1)
{
System.out.print(ia.weight+"\n");
}
HashMap<String,IPet> ip= new HashMap<String,IPet>();
ip.put(pdog.get_Name(),pdog);ip.put(pcat.get_Name(),pcat);
Iterator<String> it = ip.keySet().iterator();
while(it.hasNext()){ //判断是否还有下一个元素
System.out.println("please enter the pet's name:");
String key=it.next();
IPet value=ip.get(key);
System.out.println(key+":");value.play();
}
}
}
※ (1)迭代器遍历:iterator方法 Iterator<读取类型> it = ip.keySet().iterator(); (2)对List中某一对象属性进行排序: Collections.sort(List_Name , new comperator<对象名>)
(二)多线程
1,通过继承Thread创建线程
class Tortoise extends Thread
{
int sleepTime=0,liveLength=0;
Tortoise(int sleepTime,String name,int liveLength){
this.sleepTime=sleepTime;
this.liveLength=liveLength;
this.setName(name); // 设置线程的名字为name
}
public void run(){
while(true)
{
liveLength--;
System.out.print("@");
try{
sleep(sleepTime); // 让线程调用sleep方法进入中断状态,sleepTime毫秒后线程重新排队,等待CUP资源
}
catch(InterruptedException e){
}
if(liveLength<=0){
System.out.print(getName()+"进入死亡状态\n");
return; // 结束run方法的语句
}
}
}
}
class Rabbit extends Thread{
int sleepTime=0,liveLength;
Rabbit(int sleepTime,String name,int liveLength) {
this.sleepTime=sleepTime;
this.liveLength=liveLength;
this.setName(name); // 设置线程的名字为name
}
public void run(){
while(true){
liveLength--;
System.out.print("*");
try{
sleep(sleepTime);
// 让线程调用sleep方法进入中断状态,sleepTime毫秒后线程重新排队,等待CUP资源
}
catch(InterruptedException e){
}
if(liveLength<=0){
System.out.print(getName()+"进入死亡状态\n");
return; // 结束run方法的语句
}
}
}
}
public class ThreadExample {
public static void main(String args[ ]){
Rabbit rabbit;
rabbit=new Rabbit(4,"tutu",3); // 新建线程rabbit
Tortoise tortoise;
tortoise=new Tortoise(7,"guigui",6); // 新建线程tortoise
tortoise.start(); // 启动线程tortoise
rabbit.start(); // 启动线程rabbit
}
}
2,通过向Thread()构造方法传递Runnable对象
class Shop implements Runnable
{
Thread zhangWorker,wangWorker,boss;
Shop()
{
boss=new Thread(this);// 1创建boss,Shop对象为boss的目标对象
zhangWorker=new Thread(this); // 2创建zhangWorker,Shop对象为zhangWorker的目标对象
wangWorker=new Thread(this); // 3创建wangWorker,Shop对象为wangWorker的目标对象
zhangWorker.setName("张工");
wangWorker.setName("王工");
boss.setName("老板");
}
public void run()
{
int i=0;
if(Thread.currentThread()==zhangWorker)
{
while(true)
{
try{
i++;
System.out.println(zhangWorker.getName()+"已搬运了"+i+"箱苹果");
if(i==3)
return;
Thread.sleep(10000);//4 zhangWorker休眠10秒(10000毫秒)
}
catch(InterruptedException e){
System.out.println(boss.getName()+"让"+zhangWorker.getName()+"继续工作"); }
}
}
else if(Thread.currentThread()==wangWorker){
while(true){
try{
i++;
System.out.println(wangWorker.getName()+"已搬运了"+i+"箱香蕉");
if(i==3)
return;
Thread.sleep(10000);// 5wangWorker休眠10秒(10000毫秒)
}
catch(InterruptedException e){
System.out.println(boss.getName()+"让"+wangWorker.getName()+"继续工作"); }
}
}
else if(Thread.currentThread()==boss){
while(true)
{
zhangWorker.interrupt();// 6吵醒zhangWorker
wangWorker.interrupt(); // 7吵醒wangWorker
if(!(wangWorker.isAlive()||zhangWorker.isAlive())) {
System.out.println(boss.getName()+"下班");
return;
}
}
}
}
}
public class ShopExample {
public static void main(String args[]){
Shop shop=new Shop();
shop.zhangWorker.start();
shop.wangWorker.start();
shop.boss.start();
}
}