当前位置:网站首页>Constructor, static method, abstract method, default method in interface
Constructor, static method, abstract method, default method in interface
2022-07-21 04:14:00 【Dark circles without solution】
One . Construction method :
1. When creating an instance , In fact, it is created through the construction method .
2. The format of the construction method is :public + Class name ( Parameters ){ Method body }
3. Compared with ordinary methods , The constructor does not return a value , either void, When calling the constructor , Need to use new keyword
4. The format of creating an instance is : Class name + quote ( object ) = new Class name ( Parameters / No arguments );
5. When no parameters are defined , The constructor has a parameterless constructor by default public + Class name (){ Method body }
Be careful :( If we customize a construction method , Then there is no compiler default parameterless constructor )!!!
6. If you want to use both parametric construction methods , And want to keep the parameterless construction method , Then we can only define both nonparametric methods and parametric methods .
7. When there is no initialized field in the constructor , The default value of the reference type is null,int The default value is 0,boolean The default value is false
8. In a class , Multiple construction methods can be defined , stay new() Invocation time , According to the type of parameter , Number , Automatic matching of sequence and other suitable construction methods
9. One constructor can call other constructors , In order to realize code reuse , The statements that call other constructor methods are this( Parameters )
Two . Static methods
1. use static Methods of keyword modification , Can be called directly by class name , Do not use instance objects
The format is public/protected/private + static + void + Method name (){ Method body }
2. Static methods belong to class Class level , Does not belong to instance object , So I can't access this Variable , Also cannot access the member variables of the instance object .
3. Static methods can only access static methods or static member variables
4. Ordinary methods can access static methods or static member variables
5. You can also call static methods or static member variables through instance variables ,( The compiler automatically rewrites the instance variable to the class name , It is not recommended to use ), If you force this call , No mistake. , But a warning will be generated .
6. Execution order : Static code block --> Building blocks of code --> Constructors --> Common code block
7. Static methods in the interface call directly with the interface name ( The member variable of an interface can only be public atatic final type )
8. Static methods are often used in tool classes and auxiliary methods
3、 ... and . Common method : Modifier (public、protected、private) + Return value + Method name ( Parameter type , Parameter name ){ Method body , Realize specific functions }
Four . Abstract method :① If the method of the parent class does not need to implement any function , Just sign the definition method , The purpose is to let the self class override the methods of the parent class , that , Methods of the parent class can be declared as abstract methods
② The format of abstract methods :public/protected/private + abstract + void + Method name ();
③ Abstract methods must be in abstract classes , Abstract classes don't necessarily have abstract methods
④ When inheriting an abstract parent class , The abstract method of the parent class must be overridden ( Unless the subclass is also an abstract class , Can not rewrite )、
5、 ... and . Default method in interface :① Ordinary methods and construction methods are not allowed in the interface , But you can define a default Method ( Equivalent to ordinary method )
② The format is :public + default + void + Method name (){}
边栏推荐
猜你喜欢
随机推荐
[Unity脚本优化] Optimizing garbage collection in Unity games
HDF5 library version mismatched error的解决方案
jenkins设置语言为中文
Graduation thesis title of power system and automation [selected]
SAFERTOS在医疗设备中的应用
One bite of Stream(5)
条件判断if语句case语句
【无标题】
随机数(random头文件)
cocos2dx 引擎中的一些技巧
SNAT与DNAT
One bite of Stream(5)
UGUI——ToggleGroup
Title of graduation thesis on architectural decoration
Don't know how to learn MySQL? It's enough to finish the 50 questions of Niuke! (Part V)
网络安全学习(千锋网络安全笔记)2--IP与基本DOS命令
什麼是渲染管道,怎麼繪制3D物體
Mysql-CVE-2012-2122漏洞&Redis漏洞
请问一下 flink1.15 flinksql kafka topic如果不提前创建提交作业就会失败
不知道 MySQL 咋学?刷完牛客这 50 道题就够了!(第六篇)