package com. uncle. class_farmer ;
public class Test {
public static void main ( String [ ] args) {
Farmer farmer = new Farmer ( ) ;
Pig pig = farmer. feedPig ( 5 ) ;
Butcher butcher = new Butcher ( ) ;
butcher. killPig ( pig) ;
}
}
package com. uncle. class_car ;
public class Car {
public String brand;
public String type;
public String color;
public Wheel wheel;
public Car ( ) { }
public Car ( String brand, String type, String color, Wheel wheel) {
this . brand= brand;
this . type= type;
this . color= color;
this . wheel= wheel;
}
public void showCar ( ) {
System . out. println ( "这是一辆" + brand+ "牌" + type+ "型号" + color+ "的小汽车" ) ;
System . out. println ( "车上搭载着" + wheel. brand+ "牌的" + wheel. size+ "尺寸" + wheel. color+ "颜色的车轮子" ) ;
wheel. turn ( ) ;
}
}
package com. uncle. class_car ;
public class Wheel {
public String brand;
public int size;
public String color;
public Wheel ( ) { }
public Wheel ( String brand, int size, String color) {
this . brand = brand;
this . size = size;
this . color = color;
}
public void turn ( ) {
System . out. println ( "车轮子可以旋转" ) ;
}
}
package com. uncle. class_farmer ;
public class Test {
public static void main ( String [ ] args) {
Farmer farmer = new Farmer ( ) ;
Pig pig = farmer. feedPig ( 5 ) ;
Butcher butcher = new Butcher ( ) ;
butcher. killPig ( pig) ;
}
}
package com. uncle. class_farmer ;
public class Butcher {
public void killPig ( Pig pig) {
System . out. println ( "屠夫执行了杀猪方法" ) ;
String pigName = pig. getName ( ) ;
int pigWeight = pig. getWeight ( ) ;
System . out. println ( pigName+ "的体重为:" + pigWeight) ;
pig. beKilled ( ) ;
}
}
package com. uncle. class_farmer ;
public class Farmer {
public Pig feedPig ( int month) {
Pig pig = new Pig ( "小花" ) ;
pig. growUp ( month) ;
return pig;
}
}
package com. uncle. class_farmer ;
public class Pig {
private String name;
private int weight = 20 ;
public Pig ( ) { }
public Pig ( String name) {
this . name= name;
}
public void beKilled ( ) {
System . out. println ( this . name+ "被杀啦,好惨呀" ) ;
}
public void growUp ( int month) {
for ( int i= 1 ; i<= month; i++ ) {
this . weight*= 2 ;
}
}
public int getWeight ( ) {
return this . weight;
}
public String getName ( ) {
return this . name;
}
}
package com. uncle. class_extends. policecar_car ;
public class Test {
public static void main ( String [ ] args) {
Car car = new Car ( 22 ) ;
Velometer v = new Velometer ( 5 ) ;
v. measureCar ( car) ;
}
}
package com. uncle. class_extends. policecar_car ;
public class Car {
private int speed;
public Car ( ) { }
public Car ( int speed) {
this . speed= speed;
}
public int getSpeed ( ) {
return this . speed;
}
}
package com. uncle. class_extends. policecar_car ;
public class PoliceCar {
private int speed;
public PoliceCar ( ) { }
public PoliceCar ( int speed) {
this . speed= speed;
}
public void chase ( Car car) {
int carSpeed = car. getSpeed ( ) ;
if ( this . speed> carSpeed) {
System . out. println ( "警车开始追击" ) ;
int time = 100 / ( this . speed- carSpeed) ;
try {
Thread . sleep ( 3000 ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
System . out. println ( "小样儿跑了你啦,经过" + time+ "秒追到啦" ) ;
} else {
System . out. println ( "小汽车飞的太快啦,望尘莫及" ) ;
}
}
}
package com. uncle. class_extends. policecar_car ;
public class Velometer {
private int standardTime;
public Velometer ( ) { }
public Velometer ( int standardTime) {
this . standardTime= standardTime;
}
public void measureCar ( Car car) {
int carSpeed = car. getSpeed ( ) ;
int carTime = 100 / carSpeed;
if ( this . standardTime<= carTime) {
System . out. println ( "速度正常,请保持安全行驶,祝您一路平安" ) ;
} else {
System . out. println ( "经过测量,小气车超速啦,警车可以追击啦" ) ;
PoliceCar pc = new PoliceCar ( 25 ) ;
pc. chase ( car) ;
}
}
}
package com. uncle. class_extends. student_computer ;
public class Test {
public static void main ( String [ ] args) {
MachineRoom room = new MachineRoom ( ) ;
Student student1 = new Student ( "步尔斯特" ) ;
room. welcomeStudent ( student1) ;
Student student2 = new Student ( "张三" ) ;
room. welcomeStudent ( student2) ;
Student student3 = new Student ( "李四" ) ;
room. welcomeStudent ( student3) ;
Student student4 = new Student ( "王五" ) ;
room. welcomeStudent ( student4) ;
Student student5 = new Student ( "赵六" ) ;
room. welcomeStudent ( student5) ;
Student student6 = new Student ( "钱七" ) ;
room. welcomeStudent ( student6) ;
}
}
package com. uncle. class_extends. student_computer ;
public class Computer {
private int number;
private boolean used = false ;
public Computer ( ) { }
public Computer ( int number) {
this . number= number;
}
public int getNumber ( ) {
return this . number;
}
public boolean isUsed ( ) {
return this . used;
}
public void beOpen ( ) {
this . used = true ;
System . out. println ( this . number+ "号电脑被打开啦" ) ;
}
public void beUsing ( ) {
System . out. println ( this . number+ "号电脑正在被使用中。。。" ) ;
}
public void beClose ( ) {
this . used = false ;
System . out. println ( this . number+ "号电脑被关闭啦" ) ;
}
}
package com. uncle. class_extends. student_computer ;
public class MachineRoom {
public Computer [ ] computers = new Computer [ 5 ] ;
{
for ( int i= 0 ; i< computers. length; i++ ) {
computers[ i] = new Computer ( i+ 1 ) ;
}
}
public void welcomeStudent ( Student student) {
String studentName = student. getName ( ) ;
System . out. println ( "欢迎" + studentName+ "学生进入机房" ) ;
for ( int i= 0 ; i< computers. length; i++ ) {
boolean computersState = computers[ i] . isUsed ( ) ;
if ( ! computersState) {
student. useComputer ( computers[ i] ) ;
break ;
}
}
}
}
package com. uncle. class_extends. student_computer ;
public class Student {
private String name;
private int RP = ( int ) ( Math . random ( ) * 10 ) ;
public Student ( ) { }
public Student ( String name) {
this . name= name;
}
public String getName ( ) {
return this . name;
}
public void useComputer ( Computer computer) {
System . out. println ( this . name+ "开始使用电脑啦" ) ;
computer. beOpen ( ) ;
computer. beUsing ( ) ;
if ( this . RP< 5 ) {
computer. beClose ( ) ;
} else {
System . out. println ( this . name+ "RP有问题 没关电脑 什么素质啊~" ) ;
}
}
}
警车和小汽车 学生和机房