1:练习

image.png :::info 最后输出:9 red 100 red
注意:color是静态属性 :::

2:

image.png

  1. public class HomeWork2 {
  2. public static void main(String[] args) {
  3. System.out.println(Frock.getNextNum());
  4. System.out.println(Frock.getNextNum());
  5. Frock frock = new Frock();
  6. System.out.println(frock.getSerialNumber());
  7. Frock frock1 = new Frock();
  8. System.out.println(frock1.getSerialNumber());
  9. Frock frock2 = new Frock();
  10. System.out.println(frock2.getSerialNumber());
  11. }
  12. }
  13. class Frock{
  14. private static int currentNum = 100000;
  15. private int serialNumber;
  16. public Frock() {
  17. this.serialNumber = getNextNum();
  18. }
  19. public static int getNextNum(){
  20. return currentNum += 100;
  21. }
  22. public int getSerialNumber() {
  23. return serialNumber;
  24. }
  25. }

3:

image.png

  1. public class HomeWork3 {
  2. public static void main(String[] args) {
  3. Animal cat = new cat();
  4. Animal dog = new dog();
  5. cat.shout();
  6. dog.shout();
  7. }
  8. }
  9. abstract class Animal{
  10. abstract public void shout();
  11. }
  12. class cat extends Animal{
  13. @Override
  14. public void shout() {
  15. System.out.println("小猫叫");
  16. }
  17. }
  18. class dog extends Animal{
  19. @Override
  20. public void shout() {
  21. System.out.println("小狗叫");
  22. }
  23. }

4:

image.png

  1. package Date0821.HomeWork;
  2. public class HomeWork4 {
  3. public static void main(String[] args) {
  4. Cellphone cellphone = new Cellphone();
  5. //匿名内部类是
  6. /*
  7. new Calculate() {
  8. @Override
  9. public int Work(int n1, int n2) {
  10. System.out.println(n1 + n2);
  11. return n1 + n2;
  12. }
  13. } 同时也是一个对象
  14. */
  15. cellphone.TestWork(new Calculate() {
  16. @Override
  17. public int Work(int n1, int n2) {
  18. System.out.println(n1 + n2);
  19. return n1 + n2;
  20. }
  21. },4,5);
  22. cellphone.TestWork(new Calculate() {
  23. @Override
  24. public int Work(int n1, int n2) {
  25. System.out.println(n1 + n2);
  26. return n1 * n2;
  27. }
  28. },4,5);
  29. }
  30. }
  31. interface Calculate{
  32. //work方法完成计算,具体计算方法交给匿名内部类
  33. public int Work(int n1 ,int n2);
  34. }
  35. class Cellphone{
  36. public void TestWork (Calculate calculate,int n1,int n2){
  37. int result = calculate.Work(n1,n2);
  38. System.out.println("运算结束,结果为"+ result);
  39. }
  40. }

5:

image.png

  1. public class HomeWork5 {
  2. public static void main(String[] args) {
  3. A a = new A();
  4. //通过方法创建成员内部类,然后调用方法
  5. a.Get().show();
  6. //创建成员内部类
  7. A.B b = a.new B();
  8. b.show();
  9. }
  10. }
  11. class A {
  12. private String name = "A";
  13. class B{
  14. private final String name = "B";
  15. public void show(){
  16. System.out.println(A.this.name);
  17. System.out.println(B.this.name);
  18. }
  19. }
  20. public B Get(){
  21. B b = new B();
  22. return b;
  23. }
  24. }
  1. public class HomeWork5 {
  2. public static void main(String[] args) {
  3. A a = new A();
  4. a.Inner();
  5. }
  6. }
  7. class A {
  8. private String name = "A";
  9. public void Inner(){
  10. class B{
  11. private final String name = "B";
  12. public void show(){
  13. System.out.println(A.this.name);
  14. System.out.println(B.this.name);
  15. }
  16. }
  17. B b = new B();
  18. b.show();
  19. }
  20. }

6:

image.png

  1. package Date0821.HomeWork;
  2. /**
  3. * 作者:sakura
  4. * 日期:2022年08月21日 22:09
  5. */
  6. public class HomeWork6 {
  7. public static void main(String[] args) {
  8. //正常情况下:船每次都会换,而马这个对象是不变的
  9. //初始化人的时候会给他分配一个交通工具,但是又不能浪费
  10. Person sakura = new Person("Sakura", factory.getHorse());
  11. sakura.Commen();
  12. sakura.River();
  13. sakura.River();
  14. sakura.mountain();
  15. }
  16. }
  17. interface Vehicles{
  18. public void work();
  19. }
  20. class horse implements Vehicles{
  21. @Override
  22. public void work() {
  23. System.out.println("用马");
  24. }
  25. }
  26. class Boat implements Vehicles{
  27. @Override
  28. public void work() {
  29. System.out.println("用船");
  30. }
  31. }
  32. class factory{
  33. //马每次只需要一匹
  34. public static horse horse = new horse();
  35. //饿汉式
  36. //这里使用static就不用创建工厂对象了
  37. public static horse getHorse(){
  38. return horse;
  39. }
  40. public static Boat getBoat(){
  41. return new Boat();
  42. }
  43. }
  44. class Person{
  45. private String name;
  46. private Vehicles vehicles;
  47. public void setVehicles(Vehicles vehicles) {
  48. this.vehicles = vehicles;
  49. }
  50. public Person(String name, Vehicles vehicles) {
  51. this.name = name;
  52. this.vehicles = vehicles;
  53. }
  54. public void River(){
  55. //这个判断条件有两个含义:
  56. //1: 判断这个交通工具不是船
  57. //2: 判断这个交通功能为空
  58. if ( !(this.vehicles instanceof Boat) ){
  59. this.vehicles = factory.getBoat();
  60. }
  61. this.vehicles.work();
  62. }
  63. public void Commen(){
  64. if (!(this.vehicles instanceof horse)){
  65. this.vehicles = factory.getHorse();
  66. }
  67. this.vehicles.work();
  68. }
  69. //火焰山只需要飞过一次,所以用匿名内部类
  70. public void mountain(){
  71. new Vehicles() {
  72. @Override
  73. public void work() {
  74. System.out.println("飞过火焰山");
  75. }
  76. }.work();
  77. }
  78. }

7:

image.png

  1. public class HomeWork7 {
  2. public static void main(String[] args) {
  3. Car car = new Car();
  4. Car.Air air = car.new Air();
  5. air.flow(60);
  6. air.flow(30);
  7. air.flow(-30);
  8. }
  9. }
  10. class Car{
  11. private double temperature;
  12. class Air{
  13. public void flow(double temperature){
  14. if (temperature > 40) {
  15. System.out.println("吹冷风");
  16. }else if (temperature < 0){
  17. System.out.println("吹热风");
  18. }else {
  19. System.out.println("啥都不干");
  20. }
  21. }
  22. }
  23. //也可以通过getAir返回对象,在加一个有参构造器实现
  24. // Car car = new Car(60);
  25. // car.getAir.flow();
  26. public Air getAir(){
  27. return new Air();
  28. }
  29. }

8:

image.png

  1. public class HomeWork8 {
  2. public static void main(String[] args) {
  3. Color.RED.show();
  4. Color.GREEN.show();
  5. }
  6. }
  7. interface T{
  8. public void show();
  9. }
  10. enum Color implements T{
  11. RED(255,0,0),
  12. BLUE(0,0,255),
  13. BLACK(0,0,0),
  14. YELLOW(255,255,0),
  15. GREEN(0,255,0);
  16. private int REDValue;
  17. private int GREENValue;
  18. private int BLUeValue;
  19. Color(int REDValue, int GREENValue, int BLUeValue) {
  20. this.REDValue = REDValue;
  21. this.GREENValue = GREENValue;
  22. this.BLUeValue = BLUeValue;
  23. }
  24. @Override
  25. public void show() {
  26. System.out.println("(" + REDValue+","+GREENValue+","+ +BLUeValue+")");
  27. }
  28. }