目录
第一题
第二题
第三题
第四题
第五题
第六题
代码的问题点
第七题
第八题
第一题
package homework;
public class HomeWork01 {
public static void main(String[] args) {
Car c = new Car();//创建新对象,没有实参
Car c1 = new Car(100);
//1.创建一个新的对象实例,调用无参构造器
//2.在无参构造器中price从10.0变成9.0
//3.color从white变成red
System.out.println(c);//9.0,red
//1.创建一个新对象实例c1,调用public Car(double price)这个有参构造器
//2.c1的price = 10;调用构造器,传参之后变成了100.0
//3.color是static属性,是所有Car类的对象共享的,
// Car c = new Car();之后已经变成了red,所以对象c1的color属性也是red
System.out.println(c1);//100.0 ,red
}
}
class Car {
double price = 10;
static String color = "white";//静态属性,所有对象共享
public String toString() {//重写toString方法
return price + "\t" + color;
}
public Car() {//无参构造器
this.price = 9;
this.color = "red";
}
public Car(double price) {//有参构造器
this.price = price;
}
}
考察知识点:类变量是类中所有对象共享的
第二题
package homework;
public class HomeWork02 {
}
class Frock {
private static int currentNum = 100000;
public int serialNumber;
public static int getNextNum() {
currentNum += 100;
return currentNum;
}
public int getSerialNumber() {
return serialNumber;
}
public Frock() {
this.serialNumber = getNextNum();
}
}
class TestFrock {
public static void main(String[] args) {
System.out.println(Frock.getNextNum());
System.out.println(Frock.getNextNum());
Frock frock1 = new Frock();
Frock frock2 = new Frock();
Frock frock3 = new Frock();
System.out.println(frock1.serialNumber);
System.out.println(frock2.serialNumber);
System.out.println(frock3.serialNumber);
}
}
第三题
package homework;
public class HomeWork03 {
public static void main(String[] args) {
Animal cat = new Cat();//父类引用指向子类对象
cat.shout();
Animal dog = new Dog();
dog.shout();
}
}
abstract class Animal {
public abstract void shout();
}
class Cat extends Animal {
@Override
public void shout() {
System.out.println("猫会喵喵叫...");
}
}
class Dog extends Animal {
@Override
public void shout() {
System.out.println("狗会汪汪叫...");
}
}
第四题
考察:匿名内部类
我的代码
package homework;
public class HomeWork04 {
public static void main(String[] args) {
CellPhone cellPhone = new CellPhone();
cellPhone.testWork(new Calculator() {
@Override
public void work() {
System.out.println("调用work()方法...");
}
});
}
}
interface Calculator{
void work();
}
class CellPhone{
public void testWork(Calculator calculator){
calculator.work();
}
}
问题:没有设计运算方法
修改过后的代码
package homework;
public class HomeWork04 {
public static void main(String[] args) {
CellPhone cellPhone = new CellPhone();
cellPhone.testWork(new Calculator() {
@Override
public void work(double n1, double n2) {
System.out.println("res=" + (n1 * n2));
}
}, 5, 6);
}
}
interface Calculator{
void work(double n1,double n2);
}
class CellPhone {
public void testWork(Calculator calculator,double n1, double n2){
calculator.work(n1,n2);
}
}
需要思考的点:
public void testWork(Calculator calculator,double n1, double n2)
因为需要在testWork方法中调用Calculator的work方法,所以参数必须要有以下要素 1.Calculator类的对象实例,2.需要运算的数据
第五题
package homework;
public class HomeWork05 {
public static void main(String[] args) {
A a = new A();
a.hi();
}
}
class A{
public String NAME = "rose";
public void hi(){
class B{//局部内部类
//错误:私有常量即被final修饰的变量
private final String NAME = "jack";
public void show(){
//重名的情况调用外部类的NAME: A.this.NAME
System.out.println("name =" + NAME + " 外部类A的name=" + A.this.NAME);
}
}
//局部内部类B类的作用域在方法hi()中
//在方法hi()中创建B类的对象实例,然后访问show()方法
B b = new B();
b.show();
}
}
第六题
package homework.homework06;
interface Vehicles {
//1.有一个交通工具接口类Vehicles,有work接口
void work();
}
package homework.homework06;
public class Boat implements Vehicles{
@Override
public void work() {
System.out.println("遇到大河时用Boat作为交通工具...");
}
}
package homework.homework06;
public class Horse implements Vehicles{
//2.有Horse类和Boat类分别实现Vehicles
@Override
public void work() {
System.out.println("一般情况下用Horse作为交通工具...");
}
}
package homework.homework06;
public class VehiclesFactory {
//3.创建交通工具工厂类,有两个方法分别获得交通工具Horse和Boat
//创建方法,返回Horse和Boat的对象实例
//为了代码的便利性,直接使用 类名调用,即需要把 getHorse()设置成static方法
public static Horse getHorse() {
return new Horse();
}
public static Boat getBoat() {
return new Boat();
}
}
package homework.homework06;
public class Person {
//4.有Person类,有name和Vehicles属性,在构造器中为两个属性赋值
private String name;
private Vehicles vehicles;
public Person(String name, Vehicles vehicles) {
this.name = name;
this.vehicles = vehicles;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Vehicles getVehicles() {
return vehicles;
}
public void setVehicles(Vehicles vehicles) {
this.vehicles = vehicles;
}
//将过河和走路的方法都封装在Person类中
public void passRiver(){
//先从工厂得到一只船,然后调用work方法
Boat boat = VehiclesFactory.getBoat();
boat.work();
}
public void command(){
Horse horse = VehiclesFactory.getHorse();
horse.work();
}
}
package homework.homework06;
public class HomeWork06 {
public static void main(String[] args) {
//5.实例化Person对象“唐僧”,要求一般情况下用Horse作为交通工具,遇到大河时用Boat作为交通工具
Person person = new Person("唐僧", new Horse());
//调用平时和过河两种方法
person.passRiver();
person.command();
}
}
代码的问题点
在创建Person类的对象实例时,属性vehicles = new Horse(),但是调用command方法时,会再次创建一个Boat类对象,造成对象实例的浪费
解决办法:判断一下,当前的 vehicles属性 是否已经存在了
//将过河和走路的方法都封装在Person类中
//先从工厂得到一只船,然后调用work方法
//判断一下,当前的 vehicles == null ,如果结果为true,就获取一艘船
public void passRiver(){
if(vehicles == null){
vehicles = VehiclesFactory.getBoat();
}
vehicles.work();
}
//在创建Person类的对象实例时,属性vehicles = new Horse(),
// 但是调用command方法时,会再次创建一个Boat类对象,造成对象实例的浪费
//解决办法:判断一下,当前的 vehicles == null ,如果结果为true,就获取一匹马
public void command(){
if(vehicles == null){
//向上转型,多态
vehicles = VehiclesFactory.getHorse();
}
vehicles.work();//动态绑定
}
回到main方法中运行,结果如下
运行结果中,不论是过河还是走路都是交通工具Horse
原因:在创建对象时
vehicles = new Horse();
也就是说vehicles ≠ null,就不会获取一艘船
解决办法,对vehicles的运行类型进行判断
//将过河和走路的方法都封装在Person类中
//先从工厂得到一只船,然后调用work方法
//判断一下,当前的 vehicles == null ,如果结果为true,就获取一艘船
//如何防止始终使用的是传入的马 ,使用 instanceof 对运行类型进行判断
//vehicles == null ---> vehicles instanceof Boat = false, 取反为true
//vehicles = 马对象 ---> vehicles instanceof Boat = false , 取反为true
//以上两种都需要获取一艘船
//vehicles = 船对象 ---> vehicles instanceof Boat = true , 取反为false,不需要再获取船
public void passRiver(){
if(!(vehicles instanceof Boat)){
vehicles = VehiclesFactory.getBoat();
}
vehicles.work();
}
//在创建Person类的对象实例时,属性vehicles = new Horse(),
// 但是调用command方法时,会再次创建一个Boat类对象,造成对象实例的浪费
//解决办法:判断一下,当前的 vehicles == null ,如果结果为true,就获取一匹马
public void command(){
if(!(vehicles instanceof Horse)){
//向上转型,多态
vehicles = VehiclesFactory.getHorse();
}
vehicles.work();//动态绑定
}
运行结果
进一步优化:每次骑的都是同一匹马,但是不是一艘船,也就是说马对象是固定的,这个时候需要用到饿汉式
package homework.homework06;
public class VehiclesFactory {
private static Horse horse = new Horse();//饿汉式
private VehiclesFactory() {
}
//3.创建交通工具工厂类,有两个方法分别获得交通工具Horse和Boat
//创建方法,返回Horse和Boat的对象实例
//为了代码的便利性,直接使用 类名调用,即需要把 getHorse()设置成static方法
public static Horse getHorse() {
return horse;
}
public static Boat getBoat() {
return new Boat();
}
}
拓展:遇到火焰山时,坐飞机
package homework.homework06;
public class Plane implements Vehicles{
@Override
public void work() {
System.out.println("遇到火焰山时,乘坐飞机...");
}
}
package homework.homework06;
public class VehiclesFactory {
private static Horse horse = new Horse();//饿汉式
private VehiclesFactory() {
}
//3.创建交通工具工厂类,有两个方法分别获得交通工具Horse和Boat
//创建方法,返回Horse和Boat的对象实例
//为了代码的便利性,直接使用 类名调用,即需要把 getHorse()设置成static方法
public static Horse getHorse() {
return horse;
}
public static Boat getBoat() {
return new Boat();
}
public static Plane getPlane(){
return new Plane();
}
}
Person类
public void passFireHill(){
if(!(vehicles instanceof Plane)){
//向上转型,多态
vehicles = VehiclesFactory.getPlane();
}
vehicles.work();//动态绑定
}
package homework.homework06;
public class HomeWork06 {
public static void main(String[] args) {
//5.实例化Person对象“唐僧”,要求一般情况下用Horse作为交通工具,遇到大河时用Boat作为交通工具
Person person = new Person("唐僧", new Horse());
//调用平时和过河两种方法
person.passRiver();
person.command();
person.passRiver();
person.passRiver();
person.command();
person.passFireHill();
}
}
第七题
考察知识点:成员内部类,访问成员内部类的方式
package homework;
public class HomeWork07 {
public static void main(String[] args) {
Car_ car_1 = new Car_(10);
car_1.getAirInstance().flow();
Car_ car_2 = new Car_(-3);
car_2.getAirInstance().flow();
Car_ car_3 = new Car_(43);
car_3.getAirInstance().flow();
}
}
class Car_{
private double temperature;
public Car_(double temperature) {
this.temperature = temperature;
}
class Air{//成员内部类
public void flow(){
if(temperature > 40){
System.out.println("温度大于40度,空调吹冷风...");
}else if(temperature < 0){
System.out.println("温度小于0度,空调吹暖风...");
}else{
System.out.println("温度在0-40之间,关闭空调...");
}
}
}
//访问成员内部类
public Air getAirInstance(){
return new Air();
}
}
第八题
考察知识点
枚举类的代码
interface IA{
void show();
}
enum Color implements IA{
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;
private Color(int redValue, int greenValue, int blueValue) {
this.redValue = redValue;
this.greenValue = greenValue;
this.blueValue = blueValue;
}
@Override
public void show() {
System.out.println("redValue = " + redValue +" greenValue=" + greenValue +" blueValue=" + blueValue);
}
}
思路错误:还是用的String类数据去验证,即输入,然后再判断,并不是enum对象
正确思路如下:
public class HomeWork08 {
public static void main(String[] args) {
//单个匹配
Color red = Color.RED;
red.show();
switch (red){
case RED:
System.out.println("匹配到红色");
break;
case BLUE:
System.out.println("匹配到蓝色");
break;
case BLACK:
System.out.println("匹配到黑色");
break;
case GREEN:
System.out.println("匹配到绿色");
break;
case YELLOW:
System.out.println("匹配到黄色");
break;
default:
System.out.println("没有匹配的");
}
}
switch(red)
运行结果
同时还可以用values方法和增强循环,把所有的对象都匹配一遍
public class HomeWork08 {
public static void main(String[] args) {
//把所有对象都匹配一遍的方法
Color[] values = Color.values();
for(Color color : values){
color.show();
switch (color) {
case RED:
System.out.println("匹配到红色");
break;
case BLUE:
System.out.println("匹配到蓝色");
break;
case BLACK:
System.out.println("匹配到黑色");
break;
case GREEN:
System.out.println("匹配到绿色");
break;
case YELLOW:
System.out.println("匹配到黄色");
break;
default:
System.out.println("没有匹配的");
}
}
}
}