第一题
答案:9.0 red 100.0 red
分析:首先创建了一个无参的car对象,然后无参的car对象会去找car类。
此时:price = 10 , color = white.
无参的car对象会调用car类中的构造器,然后将price更新为9,将color的值更新为red。
此时:price = 9 , color = red.
然后又创建了一个有参的car对象。有参的catr对象会调用car类中的有参构造器。
此时将price更新为100。
注意:此时color的值依旧为red,因为color是静态变量,智慧被修改1次
此时:price = 100,color = red
因为price的类型为double,所以最后输出的值为
9.0 red
100.0 red
.
第二题
package HomeWork;
public class TestFork {
public static void main(String[] args) {
/*在TestFork类的main方法中,分两次调用getNextNum方法,获取序列号并打印输出*/
System.out.println("第一次调用,获得的序列号为"+Fork.getNextNum());
System.out.println("第二次调用,获得的序列号为"+Fork.getNextNum());
Fork fork1 = new Fork();
System.out.println("第三次调用,获得的序列号为"+fork1.getSerialNumber());
Fork fork2 = new Fork();
System.out.println("第四次调用,获得的序列号为"+fork2.getSerialNumber());
Fork fork3 = new Fork();
System.out.println("第五次调用,获得的序列号为"+fork3.getSerialNumber());
}
}
class Fork{
//4.在Frock类中声明serialNumber(序列号)属性,并提供对应的get方法
public int serialNumber;
public int getSerialNumber() {
return serialNumber;
}
//5.在Forck类的构造器中,通过调用getNextNum方法为Forck对象获取唯一序列号,赋给serialNumber属性
public Fork() {
this.serialNumber = getNextNum();
}
//1.在Frock类中生明私有的静态属性currentNum,初始值为100000,作为衣服出厂的序列号起始值
private static int currentNum = 100000; //静态属性是所有方法共享的
/*2.声明公有的静态方法getNextNum,作为生成上衣唯一序列号的方法。
* 每调用一次将currrentNum增加100,并作为返回值。
* */
public static int getNextNum(){
currentNum += 100;
return currentNum;
}
}
第三题
public class Homework03 {
public static void main(String[] args) {
Cat cat = new Cat();
cat.shout();
Dog dog = new Dog();
dog.shout();
}
}
public class Dog extends Animal {
@Override
public void shout() {
System.out.println("狗会汪汪叫");
}
}
public class Cat extends Animal {
@Override
public void shout() {
System.out.println("猫会喵喵叫");
}
}
public abstract class Animal {
public abstract void shout();
}
第四题
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,8);
}
}
//有一个手机类cellphone
//定义方法TestWork测试计算功能,调用计算接口的work方法
//2.调用cellphone对象的testwork方法,使用匿名内部类
class Cellphone {
//调用testwork方法时,直接传入了一个实现了Icalculate接口的匿名内部类即可
//该匿名内部类可以灵活的使用work,完成不同的计算任务
public void TestWork(calculator calculator,double n1,double n2){
double result = calculator.work(n1,n2);
System.out.println("计算后的结果是="+ result);
}
}
//有一个计算器接口。接口中有一个方法是计算
public interface calculator {
public double work(double n1 ,double n2);
}
第五题
public class Homework05 {
public static void main(String[] args) {
new A().f1();
}
}
/*
编一个类A,在类中定义局部内部类B,B中有一个私有final常量name,有一个方法show()打印常量name。进行测试
进阶:A中也定义一个私有的变量name,在show方法中打印测试
*/
class A {
private String NAME = "hello";
public void f1() {
class B { //局部内部类
private final String NAME = "韩顺平教育";
public void show() {
//如果内部类和外部类的属性重名,可以同 外部类.this.属性名来指定
System.out.println("NAME=" + NAME + " 外部类的name=" + A.this.NAME);
}
}
B b = new B();
b.show();
}
}
第六题
//交通工具接口类,有一个work方法
public interface Vehicles {
public void work();
}
//Horse类实现接口
public class Horse implements Vehicles{
@Override
public void work() {
System.out.println("骑马");
}
}
//Boat类实现接口
public class Boat implements Vehicles{
@Override
public void work() {
System.out.println("坐船");
}
}
package HomeWork.HomeWork06;
//交通工具工厂类,两个方法分别获取交通工具Horse和Boat
public class VehiclesFactory {
private static Horse horse = new Horse();
private VehiclesFactory(){}
public static Horse getHorse(){
return horse;
}
public static Boat getBoat(){
return new Boat();
}
public static Plane getPlane(){
return new Plane();
}
}
package HomeWork.HomeWork06;
public class Homework6 {
public static void main(String[] args) {
Person person = new Person("唐僧",new Horse());
person.common();
person.passRiver();
person.Plane();
}
}
package HomeWork.HomeWork06;
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 Boat)){
vehicles = VehiclesFactory.getHorse();
}
vehicles.work();
}
public void Plane(){
if (!(vehicles instanceof Plane)){
vehicles = VehiclesFactory.getPlane();
}
vehicles.work();
}
}
package HomeWork.HomeWork06;
//Plane实现Vehicles接口
public class Plane implements Vehicles{
@Override
public void work() {
System.out.println("飞机");
}
}
第七题
package HomeWork.HomeWork07;
public class CarInstance {
public static void main(String[] args) {
Car car = new Car(60);
car.getAir().flow();
Car car2 = new Car(-7);
car2.getAir().flow();
}
}
package HomeWork.HomeWork07;
//car类,属性temperature
public class Car {
private double tempurature;
public Car(double tempurature) {
this.tempurature = tempurature;
}
//车内有Air类,有吹风的功能flow
class Air{
/*温度超过40度吹冷气,温度低于0度吹暖气。在这之间无其他操作*/
public void flow(){
if (tempurature>40){
System.out.println("吹冷气");
} else if (tempurature<0) {
System.out.println("吹暖气");
}else {
System.out.println("无操作");
}
}
}
public Air getAir(){
return new Air();
}
}
第八题
package HomeWork.HomeWork08;
import com.sun.scenario.effect.impl.sw.java.JSWBlend_GREENPeer;
public class HomeWork {
public static void main(String[] args) {
// Color green = Color.GREEN;
Color green = Color.RED;
switch(green){
case RED:
System.out.println("匹配到红色");
case BULE:
System.out.println("匹配搭配蓝色");
case BLACK:
System.out.println("匹配到黑色");
default:
System.out.println("没有匹配到");
}
}
}
//创建一个接口,接口里面有方法show,要求color实现这个接口
interface MyInterface{
public void show();
}
//创建一个Color枚举类
enum Color implements MyInterface{
//有五个枚举值
RED(255,0,0),BULE(0,0,255),
BLACK(0,0,0),YELLOW(255,255,0),GREEN(0,255,0);
//Color有三个属性。redValue,greenValue,blueValue
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;
}
//show方法显示三个属性的值
@Override
public void show() {
System.out.println("redValue="+redValue+"greenValue="+greenValue+"buleValue="+blueValue);
}
}