当前位置:网站首页>An abstract class, internal classes and interfaces
An abstract class, internal classes and interfaces
2022-08-04 06:31:00 【W-ater】
抽象类
什么是抽象类和抽象方法
在子类和父类中,Some methods in parent classes exist only for subclasses to override,Actually never called,这时就可以用abstractkeyword defines it as an abstract method,Its parent class is also usedabstract修饰,成为抽象类.例如下面的例子:
public abstract class Animal{
//A class that holds abstract methods is called an abstract class,也要用abstract修饰
int age;
public abstract void eat();//in practical application,The purpose of this method in the parent class is to allow subclasses to override it,所以用abstract修饰成抽象方法
}
public class Dog{
public void eat(){
System.out.println("狗吃肉");
}
}
public class Cat{
public void eat(){
System.out.println("猫吃鱼");
}
}
抽象类的特点
- 抽象类中不一定有抽象方法,有抽象方法的类必须是抽象类
- Abstract methods can have ordinary methods
- 抽象类不能够实例化,But there are still constructors in abstract classes
public abstract class Animal {
int age;
public void run(){
//抽象类可以有普通方法,Because it can be inherited by subclasses
System.out.println("向前跑");
}
public abstract void eat();
}
public class Dog extends Animal{
public void eat(){
//重写父类的抽象方法
System.out.println("狗吃肉");
}
}
public class Test6 {
public static void main(String[] args) {
//Animal a=new Animal(); 报错
Dog d=new Dog();
d.age=4;
d.run();//子类Dog对象dCall a normal method inherited from the parent class
d.eat();//子类Dog对象dCall an abstract method that overrides the parent class
}
}
接口
什么是接口
An interface is a new thing different from a class,它用interface关键字来定义,Interfaces function similarly to abstract classes,But it is more abstract than abstract class
public interface Demo{
//定义一个接口
}
接口的特点
- 接口中只有抽象方法
- 接口不能创建对象
- Subclasses implementing an interface need to override all abstract methods in the interface
- Properties in an interface have only constants
public interface Demo{
public static final int COUNT=10;//接口中只有常量,即使不写,also exists by defaultpublic static final
public abstract void method();//By default there is before the methodpublic abstract
/*public void method1(){ 报错,接口中只能有抽象方法,There cannot be any method with a method body }*/
}
public class Demo1 implement Demo{
public void method(){
System.out.println("重写接口的抽象方法");
}
}
public class Test{
public static void main(String[] args){
//Demo d=new Demo(); 报错,接口不能创建对象
Demo1 d=new Demo1();
System.out.println(d.COUNT);//Call constant properties in an interface with a subclass object
System.out.println(Demo.COUNT);//Its constant properties can also be called with the interface name
}
}
内部类
成员内部类
定义位置
Member inner classes are defined inside the class,方法的外部.
调用方式
Create an object of the class in the main method,Call a variable method in a class through an object
对象的创建格式
Assume member inner classB定义在类A中,想要创建B的对象,因为B定义在A中,不能直接“看见”,所以要先创建A的对象,通过A来创建B的对象
A a=new A();
A.B b=a.new B();
Two statements can be combined into one statement
A.B b=new A().new B();
局部内部类
定义位置
Local inner classes are defined inside methods
调用方式
Local inner classes are defined inside methods,Therefore, it can only be called inside the method
对象的创建格式
The same as the normal class creation format
匿名内部类
The application scenario of anonymous inner class is:有一个接口,No subclass implementation,An anonymous inner class is used when implementing this interface in the main method
定义格式:假如有一个接口A
new A(){
//new表示创建一个对象,()Represents an abstract method in an interface
public void method(){
//重写接口A中的抽象方法
}
};//this is a statement,So end with a semicolon
综合例子:
public class Demo5 {
public class Inner{
//若public改为private,Inner类是私有的,那么在Test5中通过Demo5类创建Innerobject will report an error
int a=20;
public void method(){
//若public改为private,这个方法是私有的,那么在Test5Calling this method in a class will throw an error
System.out.println("成员内部类");
}
}
public void method(){
class Inner{
int a=30;
public void method(){
System.out.println("局部内部类");
}
}
Inner a=new Inner();//Local inner classes can only be created and used in methods,Because the local inner class is inside the method,Begin and end are follow methods
a.method();
}
}
public interface Demo5_1 {
public abstract void method();//public abstractIt will exist by default even if it is not written
int COUNT=10;
}
public class Test5 {
public static void main(String[] args) {
Demo5 d=new Demo5();
Demo5.Inner a=d.new Inner();//创建成员内部类的对象,Because the member inner class is encapsulated in the class,So create an object of this class first,Then create a member inner class object through this class
a.method();
System.out.println(Demo5_1.COUNT);
Demo5_1 d1;
d1=new Demo5_1(){
public void method(){
System.out.println("匿名内部类");
}
};//new Demo5_1(){};是一条语句,So add a semicolon
method(d1);
}
public static void method(Demo5_1 d1){
d1.method();
}
}
边栏推荐
- 典型CCN网络——efficientNet(2019-Google-已开源)
- MNIST handwritten digit recognition, sorted by from two to ten
- 亚马逊云科技Build On-Amazon Neptune基于知识图谱的推荐模型构建心得
- counting cycle
- Copy攻城狮信手”粘“来 AI 对对联
- MySQL批量修改时间字段
- Comparison of oracle's number and postgresql's numeric
- The usefulness of bind() system call
- Rules.make-适合在编辑模式下看
- arm交叉编译
猜你喜欢
安装Apache服务时出现的几个问题, AH00369,AH00526,AH00072....
YOLOV5 V6.1 详细训练方法
MNIST手写数字识别 —— 从二分类到十分类
LeetCode_Dec_2nd_Week
语音驱动嘴型与面部动画生成的现状和趋势
【论文阅读】SPANET: SPATIAL PYRAMID ATTENTION NETWORK FOR ENHANCED IMAGE RECOGNITION
Completely remove MySQL tutorial
tmux concept and usage
LeetCode_Dec_3rd_Week
安装pyspider后运行pyspider all后遇到的问题
随机推荐
MNIST Handwritten Digit Recognition - Image Analysis Method for Binary Classification
度量学习(Metric learning)—— 基于分类损失函数(softmax、交叉熵、cosface、arcface)
安装MySQL的详细步骤
MOOSE平台使用入门攻略——如何运行官方教程的例子
tensorRT教程——tensor RT OP理解(实现自定义层,搭建网络)
双向LSTM
迅雷关闭自动更新
中国联通、欧莱雅和钉钉都在争相打造的秘密武器?虚拟IP未来还有怎样的可能
Comparison of oracle's number and postgresql's numeric
题目1000:输入两个整数a和b,计算a+b的和,此题是多组测试数据
亚马逊云科技Build On-Amazon Neptune基于知识图谱的推荐模型构建心得
【论文阅读】Mining Cross-Image Semantics for Weakly Supervised Semantic Segmentation
在AWS-EC2中安装Minikube集群
基于asp.net的法律援助平台的设计与实现(附项目链接)
[CV-Learning] Linear Classifier (SVM Basics)
The Unity of ML - agents interpret parameter Settings
Brief description of database and common operation guide
"A minute" Copy siege lion log 】 【 run MindSpore LeNet model
No matching function for call to ‘RCTBridgeModuleNameForClass‘
安装pyspider后运行pyspider all后遇到的问题