package com. tao. homework. OOP_high . homework01;
public class Homework01 {
public static void main ( String [ ] args) {
Car c = new Car ( ) ;
Car c1 = new Car ( 100 ) ;
}
}
class Car {
double price = 10 ;
static String color = "white" ;
public String toString ( ) {
return price + "\t" + color;
}
public Car ( ) {
this . price = 9 ;
this . color = "red" ;
}
public Car ( double price) {
this . price = price;
}
}
package com. tao. homework. OOP_high . homework02;
public class Homework02 {
public static void main ( String [ ] args) {
System . out. println ( Frock . getNextNum ( ) ) ;
System . out. println ( Frock . getNextNum ( ) ) ;
Frock frock = new Frock ( ) ;
Frock frock1 = new Frock ( ) ;
Frock frock2 = new Frock ( ) ;
System . out. println ( frock. serialNumer) ;
System . out. println ( frock1. serialNumer) ;
System . out. println ( frock2. serialNumer) ;
}
}
class Frock {
static int currentNum = 100000 ;
int serialNumer;
public int getSerialNumer ( ) {
return getNextNum ( ) ;
}
public static int getNextNum ( ) {
currentNum = currentNum + 100 ;
return currentNum;
}
public Frock ( ) {
serialNumer = getNextNum ( ) ;
}
}
package com. tao. homework. OOP_high . homework03;
public class Test {
public static void main ( String [ ] args) {
Animal cat = new Cat ( ) ;
Animal dog = new Dog ( ) ;
cat. shout ( ) ;
dog. shout ( ) ;
}
}
abstract class Animal {
abstract void shout ( ) ;
}
class Cat extends Animal {
@Override
void shout ( ) {
System . out. println ( "猫会喵喵叫" ) ;
}
}
class Dog extends Animal {
@Override
void shout ( ) {
System . out. println ( "狗会汪汪叫" ) ;
}
}
package com. tao. homework. OOP_high . homework04;
public class Homework04 {
public static void main ( String [ ] args) {
Cellphone cellphone = new Cellphone ( ) ;
cellphone. testWork ( ) ;
}
}
interface Computer {
void work ( ) ;
}
class Cellphone implements Computer {
public void testWork ( ) {
work ( ) ;
}
public void work ( ) {
System . out. println ( "手机正在工作..." ) ;
}
}
package com. tao. homework. OOP_high . homework04;
public class Homework04_02 {
public static void main ( String [ ] args) {
Cellphone02 cellphone02 = new Cellphone02 ( ) ;
cellphone02. testWork ( new ICalculate ( ) {
@Override
public double work ( double n1, double n2) {
return n1 + n2;
}
} , 10 , 8 ) ;
cellphone02. testWork ( new ICalculate ( ) {
@Override
public double work ( double n1, double n2) {
return n1 * n2;
}
} , 1 , 2 ) ;
}
}
interface ICalculate {
public double work ( double n1, double n2) ;
}
class Cellphone02 {
public void testWork ( ICalculate iCalculate, double n1, double n2) {
double result = iCalculate. work ( n1, n2) ;
System . out. println ( "计算后的结果是 = " + result) ;
}
}
package com. tao. homework. OOP_high . homework05;
public class Homework05 {
public static void main ( String [ ] args) {
new A ( ) . f1 ( ) ;
}
}
class A {
private String NAME = "A类私有变量" ;
public void f1 ( ) {
class B {
private static final String NAME = "局部内部类常量" ;
public void show ( ) {
System . out. println ( NAME + "\n" + A . this . NAME) ;
}
}
B b = new B ( ) ;
b. show ( ) ;
}
}
package com. tao. homework. OOP_high . homework06;
public class Homework06 {
public static void main ( String [ ] args) {
Person tang = new Person ( "唐僧" , new Horse ( ) ) ;
tang. common ( ) ;
tang. passRiver ( ) ;
}
}
interface Vehicles {
void work ( ) ;
}
class Horse implements Vehicles {
@Override
public void work ( ) {
System . out. println ( "一般情况 - 骑马" ) ;
}
}
class Boat implements Vehicles {
@Override
public void work ( ) {
System . out. println ( "过河情况 - 划船" ) ;
}
}
class VehiclesFactory {
public static Horse getHorse ( ) {
return new Horse ( ) ;
}
public static Boat getBoat ( ) {
return new Boat ( ) ;
}
}
class Person {
private String name;
private Vehicles vehicles;
public Person ( String name, Vehicles vehicles) {
this . name = name;
this . vehicles = vehicles;
}
public void passRiver ( ) {
if ( ! ( vehicles instanceof Boat ) ) {
vehicles = VehiclesFactory . getBoat ( ) ;
}
vehicles. work ( ) ;
}
public void common ( ) {
if ( ! ( vehicles instanceof Horse ) ) {
vehicles = VehiclesFactory . getHorse ( ) ;
}
vehicles. work ( ) ;
}
}
package com. tao. homework. OOP_high . homework07;
public class homework07 {
public static void main ( String [ ] args) {
Car car = new Car ( 41 ) ;
Car car1 = new Car ( - 1 ) ;
Car car2 = new Car ( 20 ) ;
car. getAir ( ) . flow ( ) ;
car1. getAir ( ) . flow ( ) ;
car2. getAir ( ) . flow ( ) ;
}
}
class Car {
double temperature;
public Car ( double temperature) {
this . temperature = temperature;
}
class Air {
public void flow ( ) {
if ( temperature > 40 ) {
System . out. println ( "吹冷气..." ) ;
} else if ( temperature < 0 ) {
System . out. println ( "吹暖气..." ) ;
} else {
System . out. println ( "空调关闭" ) ;
}
}
}
public Air getAir ( ) {
return new Air ( ) ;
}
}
package com. tao. homework. OOP_high . homework08;
public class Homework08 {
public static void main ( String [ ] args) {
Color green = Color . GREEN;
green. show ( ) ;
switch ( green) {
case YELLOW:
System . out. println ( "匹配到黄色" ) ;
green. show ( ) ;
break ;
case BLACK:
System . out. println ( "匹配到黑色" ) ;
break ;
default :
System . out. println ( "没有匹配到" ) ;
}
}
}
enum Color implements I {
RED ( 255 , 0 , 0 ) , BLUE ( 0 , 0 , 255 ) ,
BLACK ( 0 , 0 , 0 ) , YELLOW ( 255 , 255 , 0 ) ,
GREEN ( 0 , 255 , 0 ) ;
private int redValue;
private int greenValue;
private int blueValue;
Color ( int redValue, int greenValue, int blueValue) {
this . redValue = redValue;
this . greenValue = greenValue;
this . blueValue = blueValue;
}
@Override
public void show ( ) {
System . out. println ( "属性值为:" + redValue + "," + greenValue + "," + blueValue) ;
}
}
interface I {
void show ( ) ;
}