diff --git a/README.md b/README.md index b1901cc6f8e15250a33f6af8b5cdcb57b022030a..2f7706da31fa566c928e2df5ace114eef80f06d7 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,12 @@ 配套文章 [【七日阅书】1 计算思维和Java特性《Java程序设计与计算思维》](https://blog.csdn.net/diandianxiyu_geek/article/details/121660902?spm=1001.2014.3001.5501) +[【七日阅书】2 Java的数据类型和流程控制《Java程序设计与计算思维》](https://blog.csdn.net/diandianxiyu_geek/article/details/121681529?spm=1001.2014.3001.5501) + +[【七日阅书】3 字符串、数组、集合、泛型《Java程序设计与计算思维》](https://blog.csdn.net/diandianxiyu_geek/article/details/121703793?spm=1001.2014.3001.5501) + + + + + + diff --git a/demo.iml b/demo.iml index 3c8ec27f5b873264beeebf91fc71512e186f382b..29c7a4c41bba61c3ca21ed1470bf5f08c0731fe8 100644 --- a/demo.iml +++ b/demo.iml @@ -1,19 +1,5 @@ - - - - - - - - - - - - - - diff --git a/src/main/java/com/example/demo/controller/ClassController.java b/src/main/java/com/example/demo/controller/ClassController.java new file mode 100644 index 0000000000000000000000000000000000000000..c2c2f13b9823556c9a7a42f6c73b16bca97cc723 --- /dev/null +++ b/src/main/java/com/example/demo/controller/ClassController.java @@ -0,0 +1,39 @@ +package com.example.demo.controller; + +import com.example.demo.tool.Cat; +import com.example.demo.tool.RagdollCat; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/") +public class ClassController { + + @GetMapping("/class") + public void classDemo(){ + //对象就是类的实例化 + Cat tangguo = new Cat(); + String catName1 = tangguo.formatName(); + System.out.print(catName1); + System.out.print("\r\n"); + tangguo.setAge(3); //私有变量通过方法赋值 + tangguo.name = "糖果"; //公开变量可以直接赋值 + String catName = tangguo.formatName(); + System.out.print(catName); + System.out.print("\r\n"); + tangguo.printAge(); + } + + @GetMapping("/extends") + public void extendsDemo(){ + RagdollCat ragdollCat = new RagdollCat(); + ragdollCat.name = "小布"; + ragdollCat.say(); + String catName = ragdollCat.formatName(); + System.out.print(catName); + System.out.print("\r\n"); + + System.out.print + } +} diff --git a/src/main/java/com/example/demo/tool/Cat.java b/src/main/java/com/example/demo/tool/Cat.java new file mode 100644 index 0000000000000000000000000000000000000000..293fe009fa0d8e47a451590af1c8ffc59b7eec03 --- /dev/null +++ b/src/main/java/com/example/demo/tool/Cat.java @@ -0,0 +1,24 @@ +package com.example.demo.tool; + +public class Cat { //公共的类 + + public Cat(){ //构造函数 + System.out.print("我是构造函数,实例化的时候自动调用\r\n"); + name = "默认猫名字"; + } + + private int age = 1; //私有变量 默认值 + public String name; //公开变量 + + public void setAge(int age){ + this.age = age; + } + + public void printAge(){ + System.out.print(age); + } + + public String formatName(){ //公开方法 + return "猫的名字是:"+ name; //返回值 + } +} diff --git a/src/main/java/com/example/demo/tool/RagdollCat.java b/src/main/java/com/example/demo/tool/RagdollCat.java new file mode 100644 index 0000000000000000000000000000000000000000..d36a3b1891719012fa845f69b59bb209e7780f52 --- /dev/null +++ b/src/main/java/com/example/demo/tool/RagdollCat.java @@ -0,0 +1,17 @@ +package com.example.demo.tool; + +/** + * 布偶猫是猫的子类,继承了猫类 + */ +public class RagdollCat extends Cat { + + //重写了方法 + public String formatName(){ + return "布偶猫的名字是:"+ name + "\r\n"; + } + + //子类扩展出了新的方法 + public void say(){ + System.out.print("我是布偶猫\r\n"); + } +}