提交 aaa61e68 编写于 作者: X xiaoyu

add abstract interface

上级 89be344a
package com.example.demo.controller;
import com.example.demo.tool.TianjinCat;
import com.example.demo.tool.TjCat;
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 AbstractController {
@GetMapping("/abstract")
public void abstractDemo(){
TjCat tjCat = new TjCat();
tjCat.run();
tjCat.jump();
tjCat.kuaiban();
}
@GetMapping("/interface")
public void interfaceDemo(){
TianjinCat tianjinCat = new TianjinCat();
tianjinCat.eat();
tianjinCat.jump();
tianjinCat.run();
}
}
...@@ -33,7 +33,5 @@ public class ClassController { ...@@ -33,7 +33,5 @@ public class ClassController {
String catName = ragdollCat.formatName(); String catName = ragdollCat.formatName();
System.out.print(catName); System.out.print(catName);
System.out.print("\r\n"); System.out.print("\r\n");
System.out.print
} }
} }
package com.example.demo.tool;
/**
* 猫的抽象类
*/
abstract class CatAbstract {
//构造函数
CatAbstract(){
System.out.print("我是抽象类的构造函数\r\n");
}
abstract public void jump(); //抽象方法,没有具体实现
public void run(){
System.out.print("我是抽象类的函数,跑起来了!\r\n");
}
}
package com.example.demo.tool;
/**
* 猫的接口类
*/
interface CatInterface {
void run(); //由于必须所有方法都是public,所以public没必要写上
void jump();
}
package com.example.demo.tool;
interface CatInterface2 {
int tailNum = 1;
void eat();
}
package com.example.demo.tool;
/**
* 接口类的实现,支持实现多接口
*/
public class TianjinCat implements CatInterface,CatInterface2 {
@Override
public void run() {
System.out.print("我可跑了啊\r\n");
}
@Override
public void jump() {
System.out.print("我有"+ String.valueOf(tailNum) +"条尾巴!\r\n");
System.out.print("我可跳了啊\r\n");
}
@Override
public void eat() {
System.out.print("我可吃了啊\r\n");
}
}
package com.example.demo.tool;
/**
* 天津猫是抽象猫的实现,我家的猫就是从天津拿来的
*/
public class TjCat extends CatAbstract {
@Override
public void jump() {
System.out.print("我可跳了啊\r\n");
}
public void kuaiban(){
System.out.print("我可会打快板\r\n");
}
@Override
public void run() {
super.run();
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册