public class Car {
double price = 10;
static String color = "white";
public String toString(){
return price + "t" +color;
}
public Car(){
this.price = 9;
this.color = "black";
}
public Car(double price){
this.price = price;
}
}
public class HomeWork01 {
public static void main(String[] args) {
Car car = new Car();
Car car1 = new Car(100);
System.out.println(car);
System.out.println(car1);
}
}
2.编程题,考察静态属性,类加载
public class Frock {
private static int currentNum = 10000;
private int serialNumber;
public static int getNextNum(){
currentNum +=100;
return currentNum;
}
public Frock() {
this.serialNumber = getNextNum();
}
public int getSerialNumber() {
return serialNumber;
}
}
public class TestFrock {
public static void main(String[] args) {
Frock frock = new Frock();
System.out.println(frock.getSerialNumber());
Frock frock2 = new Frock();
System.out.println(frock2.getSerialNumber());
Frock frock3 = new Frock();
System.out.println(frock3.getSerialNumber());
}
}
输出:
10100
10200
10300
3.编程题,考查抽象类的使用
public abstract class Animal {
void shout() {
}
}
public class Cat extends Animal{
@Override
void shout() {
System.out.println("猫会喵喵叫");
}
}
public class Dog extends Animal{
@Override
void shout() {
System.out.println("狗汪汪叫");
}
}
public class HomeWork03 {
public static void main(String[] args) {
Cat cat = new Cat();
cat.shout();
Dog dog = new Dog();
dog.shout();
}
}
输出:
猫会喵喵叫
狗汪汪叫
4.编程题,考查匿名内部类的使用
public interface Calculator {
double work(double n1, double n2);
}
public class CellPhone {
public void testWork(Calculator calculator,double n1,double n2){
double result = calculator.work(n1,n2);
System.out.println("计算后的结果:" + result);
}
}
public class HomeWork04 {
public static void main(String[] args) {
CellPhone cellPhone = new CellPhone();
cellPhone.testWork(new Calculator() {
@Override
public double work(double n1, double n2) {
return n1 + n2;
}
},10,20);
//lambda表达式写法,后面介绍
CellPhone cellPhone2 = new CellPhone();
cellPhone2.testWork((n1,n2) -> {
double result = n1 +n2;
return result;
}, 10,20);
}
}
输出:
计算后的结果:30.0
计算后的结果:200.0
计算后的结果:30.0
5.编程题,考查内部类的属性访问
public class HomeWork05 {
public static void main(String[] args) {
A a = new A();
a.f1();
}
}
class A{
private String NAME = "hello";
public void f1(){
class B{
private final String NAME="小明";
public void show(){
System.out.println("内部类:" + NAME);
System.out.println("外部类:" + A.this.NAME);
}
}
B b = new B();
b.show();
}
}
6.编程题,考查单例、类型转换、工厂模式
public interface Vehicles {
public void work();
}
public class Horse implements Vehicles{
@Override
public void work() {
System.out.println("骑马。。。");
}
}
public class Boat implements Vehicles{
@Override
public void work() {
System.out.println("驾船。。。");
}
}
public 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();
}
}
public class HomeWork06 {
public static void main(String[] args) {
Person person = new Person("唐僧",new Horse());
person.common();
person.passRiver();
person.common();
person.common();
person.passRiver();
}
}
输出:
骑马。。。
驾船。。。
骑马。。。
骑马。。。
驾船。。。
7.编程题,考查内部类(成员内部类)
public class HomeWork07 {
public static void main(String[] args) {
Car2 car2 = new Car2(20);
car2.new Air().flow();
car2.setTemparature(-5);
car2.new Air().flow();
car2.setTemparature(45);
car2.new Air().flow();
}
}
class Car2{
private double temparature;
public Car2(double temparature) {
this.temparature = temparature;
}
class Air{
public void flow(){
if(temparature>40){
System.out.println("吹冷风。。。");
}else if(temparature<0){
System.out.println("吹热风。。。");
}else{
System.out.println("关闭空调");
}
}
}
public double getTemparature() {
return temparature;
}
public void setTemparature(double temparature) {
this.temparature = temparature;
}
}
8.编程题,考察枚举类的使用
public class HomeWork08 {
public static void main(String[] args) {
Color green = Color.GREEN;
green.show();
switch (green){
case RED:
System.out.println("红色");
break;
case Blue:
System.out.println("蓝色");
break;
case GREEN:
System.out.println("绿色");
break;
default:
System.out.println("未匹配到");
}
}
}
enum Color implements IEnum{
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;
}
public int getRedValue() {
return redValue;
}
public int getGreenValue() {
return greenValue;
}
public int getBlueValue() {
return blueValue;
}
@Override
public void show() {
System.out.println("红:" + redValue);
System.out.println("蓝:" + blueValue);
System.out.println("绿:" + greenValue);
}
}
interface IEnum{
void show();
}



