当前位置:网站首页>Inherit day01
Inherit day01
2022-07-06 03:03:00 【Dachang whole stack】
1/* Inherit : Extract commonalities upward , Define the same content in the parent class public class Subclass extends Parent class { // Member variables + Member method + Construction method } 2. Subclasses can use the parent class except private The content beyond decoration and construction methods */ public class Demo01Extends { public static void main(String[] args) { // Create subclass Teacher Class object Teacher t = new Teacher(); // Assign a value to a member variable t.name = " Brother Xiang "; t.salary/birthday = 66666; t.show(); // } }
2
/* Access characteristics of member variables in inheritance 1. Member variables without duplicate names (1) Subclasses have their own : Give priority to the subclass's own (2) Subclasses don't have : Find parent (3) Be careful : Subclasses can find their parents , But a parent class cannot find a child class 2. Member variables with duplicate names (1) Methods the internal , Write the variable name directly Start from the inside of the method and look up There are : Use it directly There is no : Look up , The member position of this class The member positions of this class are : Use it directly The member position of this class has no : Look up , The member position of the parent class (2) Methods the internal , direct writing this. Variable name Start from the member position of this class and look up The member positions of this class are : Use it directly The member position of this class has no : Look up , The member position of the parent class (3) Methods the internal , direct writing super. Variable name Start from the parent class member position and look up The member positions of the parent class are : Use it directly The member position of the parent class does not : Keep looking up (4) summary : Nearby principle this. Variable name : Represents the member variable of the current class this.num
super. Variable name : Member variables of the parent class super.num
*/
Access characteristics of member methods in inheritance 1. Subclasses have their own : Give priority to the subclass's own 2. Subclasses don't have : Look up for the parent class 3. How to duplicate names ( Method rewriting ): Give priority to the subclass's own
Method rewriting 1. Concept : Subclass appears as like as two peas ( return type , The method name and parameter list are the same ), There will be coverage , Also known as rewriting or duplicating . The statement remains unchanged , Re actualize . Necessary condition : Method name and parameter list must be the same Optional conditions : Return value types can be inconsistent ( Later on ) 2. The simplest way to rewrite the form : Subclass method declaration ( Define the first line of the method ) It is exactly the same as the parent method declaration [email protected] annotation : Used to detect subclass methods , Whether it is an override of the parent method 4. Distinguish from method overloading : Method overloading : Is to save namespaces Method overload requirements : The method name is the same , Different parameter list ( Different types , The quantity is different , The order of several different types is different ) 5. Method overrides the shortcut key : ctrl + o --> Select the method to override --> ok
Call the member method of the parent class directly : super. Member method name ( parameter list ...)
//@Override public void show(int num) { System.out.println("Zi03...show...num..." + num); } @Override // Don't complain : Is an override of the parent class method , Because the method names are the same , The parameter list is the same public void show() { System.out.println("Zi03...show..."); } // stay Zi03 Two appear in the class show Method , Don't complain // reason : The same name , Different parameter list , Constitutes an overloaded relationship
public class NewPhone extends OldPhone { @Override public void show() { //System.out.println("110 Incoming call ..."); // Show phone number , In fact, the father of show Method has implemented this function , // Subclasses don't need to re implement themselves , Directly call the function of the parent class super.show(); System.out.println(" Show where you belong : dongguan ..."); System.out.println(" Show Faces : "); } }
// Full parameter structure public Zi05(int num) { /* If you don't write it yourself super Call the parent class construct , Hide offer super() Call the null parameter construction of the parent class */ // But suggestions : Zikong ginseng call Null parameter of parent class // But suggestions : Zi man can call The parent class is full super(num); System.out.println(" Zi man can ..."); //super(num);// error : The calling parent class construction must be written on the first line }
// Create subclass objects : Space parameter structure Zi05 zi = new Zi05(); // Create subclass objects : Full parameter structure Zi05 zi2 = new Zi05(100);
/* Access characteristics of construction methods in inheritance 1. The name of the constructor is the same as the class name . So the subclass can't inherit the parent class construction method 2. Subclass inherits parent , Is to use the contents of the parent class , So when subclasses create objects and call construction methods , You must first call the constructor of the parent class , Complete the initialization of parent class members , Subclasses can only use members of the parent class , super() Represents the null parameter construction of the calling parent class 3. If the construction method of subclasses is not given manually super Call the parent class construct , By default, the compiler provides a super() Call the null parameter construction of the parent class 4.super Call the parent class construct , It can only be written in the first sentence 5. Constructors can be overloaded , therefore : super(...): Call the constructor of the parent class with parameters // Create subclass objects : Space parameter structure Zi05 zi = new Zi05(); // Create subclass objects : Full parameter structure Zi05 zi2 = new Zi05(100); super Usage of : 1.super. Member variable name : Member variables representing the parent class 2.super. Member method ( parameter list ...): On behalf of calling the member method of the parent class 3.super( parameter list ...): Represents calling the constructor of the parent class this Usage of : 1.this. Member variable name : Represents the member variables of this class ( I didn't find my father ) 2.this. Member method ( parameter list ...): Represents the member method of this class ( I didn't find my father ) 3.this( parameter list ...): On behalf of calling other constructor methods of this class --- Later on */
public class Fu05 { private int num; // Space parameter structure public Fu05(){ System.out.println(" Parent empty parameter ..."); } // Full parameter structure public Fu05(int num) { System.out.println(" Father man can ..."); this.num = num; } //get and set Method public void setNum(int num) { this.num = num;// super(num); public int getNum() { return num; } }
/* If you don't write it yourself super Call the parent class construct , Hide offer super() Call the null parameter construction of the parent class */ // But suggestions : Zikong ginseng call Null parameter of parent class // But suggestions : Zi man can call The parent class is full
/* Define parent employees Employee class 1. All member variables private modification 2. Provide empty parameters / Full parameter construction method 3. Provide get/set Method 4 // Unique method : */ // Create subclass Teacher Class object : Full parameter structure // Teacher t = // new Teacher(" Brother Xiang ",18,66666,33333); // // // Calling a member method // t.show(); // t.teaching(); // System.out.println("-----------"); // // // Create subclass Manager Class object : Space parameter structure // Manager m = new Manager(); // // // Assign a value to a member variable // m.setName(" Ling elder sister "); // m.setAge(16); // m.setSalary(88888); public class Employee { // private Member variables // Member method : Display information //2. Provide null parameter construction method //2. Provide null parameter construction method //3. Provide get/set Method }
/* Inheritance characteristics of classes 1.Java Only single inheritance is supported , Multiple inheritance is not supported . //Java Classes in can inherit father and son at multiple levels 2.Java Support multi-level inheritance ( Inheritance system ). 3. All classes inherit directly or indirectly Object class ,Object Class is the ultimate parent of all classes . */ // error : A class can only directly inherit one parent class
public abstract class Animal { // Member variables // Space parameter structure // Full parameter structure // The way to present information // Abstract method : eat // Abstract method : sleep //get and set Method }
public abstract class Animal { // Abstract method : eat public abstract void eat(); // Abstract method : sleep public abstract void sleep(); }
/* The difference between the definition of the abstract parent class and the previous ordinary class ? 1. You can't create objects directly 2. Abstract methods can be defined internally */
/* abstract : 2. The significance of abstract methods in abstract classes : Force the ability that subclasses must have 3. Definition format of abstract class : public abstract class Class name { ... } 4. Definition format of abstract method :
// error : You cannot directly create objects of abstract classes //Animal a = new Animal(); // establish Cat Class object Cat cat = new Cat(); // Subclass objects call member methods cat.eat(); cat.sleep();
Modifier abstract return type Method name ( parameter list ...); Be careful : (1) The method of definition is the same as before , But get rid of it {}, add to abstract keyword (2) The return value type and parameter list are determined according to the requirements (3) Classes with abstract methods , Must be defined as an abstract class , But abstract classes do not necessarily contain abstract methods 5. Use of abstract classes (1) You cannot directly create objects of abstract classes (2) Defining subclasses , Inherit Abstract parent class (3) Override all abstract methods in the abstract parent class in the subclass Get rid of abstract keyword , add to {} Shortcut key : ctrl + i (4) Create subclass objects (5) Subclass objects call methods */
//-----------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
/* Notes for abstract classes : (3) Classes with abstract methods , Must be defined as an abstract class , But abstract classes do not necessarily contain abstract methods 4. A subclass of an abstract class , All abstract methods in the abstract parent class must be overridden , otherwise , Compile failed and error reported . Unless the subclass is also an abstract class 3. In an abstract class , It doesn't have to contain abstract methods , But a class with abstract methods must be an abstract class see : MyAbstractClass 1. Abstract class cannot create object , If you create , Compile failed and error reported . Only objects with non Abstract subclasses can be created 2. In an abstract class , There has to be a construction method , Is used by subclasses to create objects , Initializes the
//-----------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
//---------------------------------------------------------------------------------
*/
// error : You cannot directly create objects of abstract classes //Animal a = new Animal(); // establish Cat Class object : Space parameter structure Cat cat = new Cat(); // call set Method to assign a value to a member variable cat.setName(" floret "); cat.setWeight(10);
problem : Animal There are two abstract methods eat and sleep, But for the current subclass , Only one abstract method is overwritten eat, Equivalent to the subclass itself has an abstract method sleep The current subclass must be defined as an abstract class public abstract void sleep();
边栏推荐
- MySQL advanced notes
- CSP date calculation
- 2.11 simulation summary
- [network security interview question] - how to penetrate the test file directory through
- [concept] Web basic concept cognition
- 华为、H3C、思科命令对比,思维导图形式从基础、交换、路由三大方向介绍【转自微信公众号网络技术联盟站】
- What are the principles of software design (OCP)
- 纯Qt版中国象棋:实现双人对战、人机对战及网络对战
- Solution: attributeerror: 'STR' object has no attribute 'decode‘
- JS regular filtering and adding image prefixes in rich text
猜你喜欢
"Hands on learning in depth" Chapter 2 - preparatory knowledge_ 2.5 automatic differentiation_ Learning thinking and exercise answers
#PAT#day10
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 22
[Yu Yue education] basic reference materials of digital electronic technology of Xi'an University of Technology
My C language learning records (blue bridge) -- files and file input and output
IPv6 jobs
RobotFramework入门(一)简要介绍及使用
Résumé des méthodes de reconnaissance des caractères ocr
4. File modification
Game theory matlab
随机推荐
I sorted out a classic interview question for my job hopping friends
张丽俊:穿透不确定性要靠四个“不变”
Is there a completely independent localization database technology
Rust language -- iterators and closures
C language - Blue Bridge Cup - promised score
Briefly describe the implementation principle of redis cluster
NR modulation 1
Zhang Lijun: penetrating uncertainty depends on four "invariants"
Communication between microservices
解决:AttributeError: ‘str‘ object has no attribute ‘decode‘
tcpdump: no suitable device found
微软语音合成助手 v1.3 文本转语音工具,真实语音AI生成器
Software design principles
Codeworks 5 questions per day (1700 average) - day 6
Differences and application scenarios between resulttype and resultmap
[kubernetes series] learn the exposed application of kubernetes service security
Master data management theory and Practice
故障分析 | MySQL 耗尽主机内存一例分析
Daily question brushing plan-2-13 fingertip life
Large scale DDoS attacks take Myanmar offline