提交 89be344a 编写于 作者: 小雨青年's avatar 小雨青年

add 类

上级 a027e42d
......@@ -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)
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="web" name="Web">
<configuration>
<webroots />
<sourceRoots>
<root url="file://$MODULE_DIR$/src/main/java" />
<root url="file://$MODULE_DIR$/src/main/resources" />
</sourceRoots>
</configuration>
</facet>
<facet type="Spring" name="Spring">
<configuration />
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
......
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
}
}
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; //返回值
}
}
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");
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册