1:练习
:::info
最后输出:9 red 100 red
注意:color是静态属性
:::
2:
public class HomeWork2 {
public static void main(String[] args) {
System.out.println(Frock.getNextNum());
System.out.println(Frock.getNextNum());
Frock frock = new Frock();
System.out.println(frock.getSerialNumber());
Frock frock1 = new Frock();
System.out.println(frock1.getSerialNumber());
Frock frock2 = new Frock();
System.out.println(frock2.getSerialNumber());
}
}
class Frock{
private static int currentNum = 100000;
private int serialNumber;
public Frock() {
this.serialNumber = getNextNum();
}
public static int getNextNum(){
return currentNum += 100;
}
public int getSerialNumber() {
return serialNumber;
}
}
3:
public class HomeWork3 {
public static void main(String[] args) {
Animal cat = new cat();
Animal dog = new dog();
cat.shout();
dog.shout();
}
}
abstract class Animal{
abstract public void shout();
}
class cat extends Animal{
@Override
public void shout() {
System.out.println("小猫叫");
}
}
class dog extends Animal{
@Override
public void shout() {
System.out.println("小狗叫");
}
}
4:
package Date0821.HomeWork;
public class HomeWork4 {
public static void main(String[] args) {
Cellphone cellphone = new Cellphone();
//匿名内部类是
/*
new Calculate() {
@Override
public int Work(int n1, int n2) {
System.out.println(n1 + n2);
return n1 + n2;
}
} 同时也是一个对象
*/
cellphone.TestWork(new Calculate() {
@Override
public int Work(int n1, int n2) {
System.out.println(n1 + n2);
return n1 + n2;
}
},4,5);
cellphone.TestWork(new Calculate() {
@Override
public int Work(int n1, int n2) {
System.out.println(n1 + n2);
return n1 * n2;
}
},4,5);
}
}
interface Calculate{
//work方法完成计算,具体计算方法交给匿名内部类
public int Work(int n1 ,int n2);
}
class Cellphone{
public void TestWork (Calculate calculate,int n1,int n2){
int result = calculate.Work(n1,n2);
System.out.println("运算结束,结果为"+ result);
}
}
5:
public class HomeWork5 {
public static void main(String[] args) {
A a = new A();
//通过方法创建成员内部类,然后调用方法
a.Get().show();
//创建成员内部类
A.B b = a.new B();
b.show();
}
}
class A {
private String name = "A";
class B{
private final String name = "B";
public void show(){
System.out.println(A.this.name);
System.out.println(B.this.name);
}
}
public B Get(){
B b = new B();
return b;
}
}
public class HomeWork5 {
public static void main(String[] args) {
A a = new A();
a.Inner();
}
}
class A {
private String name = "A";
public void Inner(){
class B{
private final String name = "B";
public void show(){
System.out.println(A.this.name);
System.out.println(B.this.name);
}
}
B b = new B();
b.show();
}
}
6:
、
package Date0821.HomeWork;
/**
* 作者:sakura
* 日期:2022年08月21日 22:09
*/
public class HomeWork6 {
public static void main(String[] args) {
//正常情况下:船每次都会换,而马这个对象是不变的
//初始化人的时候会给他分配一个交通工具,但是又不能浪费
Person sakura = new Person("Sakura", factory.getHorse());
sakura.Commen();
sakura.River();
sakura.River();
sakura.mountain();
}
}
interface Vehicles{
public 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 factory{
//马每次只需要一匹
public static horse horse = new horse();
//饿汉式
//这里使用static就不用创建工厂对象了
public static horse getHorse(){
return horse;
}
public static Boat getBoat(){
return new Boat();
}
}
class Person{
private String name;
private Vehicles vehicles;
public void setVehicles(Vehicles vehicles) {
this.vehicles = vehicles;
}
public Person(String name, Vehicles vehicles) {
this.name = name;
this.vehicles = vehicles;
}
public void River(){
//这个判断条件有两个含义:
//1: 判断这个交通工具不是船
//2: 判断这个交通功能为空
if ( !(this.vehicles instanceof Boat) ){
this.vehicles = factory.getBoat();
}
this.vehicles.work();
}
public void Commen(){
if (!(this.vehicles instanceof horse)){
this.vehicles = factory.getHorse();
}
this.vehicles.work();
}
//火焰山只需要飞过一次,所以用匿名内部类
public void mountain(){
new Vehicles() {
@Override
public void work() {
System.out.println("飞过火焰山");
}
}.work();
}
}
7:
public class HomeWork7 {
public static void main(String[] args) {
Car car = new Car();
Car.Air air = car.new Air();
air.flow(60);
air.flow(30);
air.flow(-30);
}
}
class Car{
private double temperature;
class Air{
public void flow(double temperature){
if (temperature > 40) {
System.out.println("吹冷风");
}else if (temperature < 0){
System.out.println("吹热风");
}else {
System.out.println("啥都不干");
}
}
}
//也可以通过getAir返回对象,在加一个有参构造器实现
// Car car = new Car(60);
// car.getAir.flow();
public Air getAir(){
return new Air();
}
}
8:
public class HomeWork8 {
public static void main(String[] args) {
Color.RED.show();
Color.GREEN.show();
}
}
interface T{
public void show();
}
enum Color implements T{
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+")");
}
}