Java programming-this and super
8.8 this and super
The following describes how to use the this and super keywords. By using the this and super keywords in a program, you can easily reference a lot of content inside the class and understand the implementation principle of object-oriented, it is easier to understand the
Super keywordIn a subclass that represents a reference to a parent class object, you can call the properties of a method in the parent class in a subclass.Super Statement---Subclass after inheriting the parent class, the child class will have a super statement in its construction method.If the super statement is not manually specified, the parent class is called
1. Subclass constructors If you want to refer to super, you must put super in the first place of the function.class Base {Base () {System. out. println ("Base");}} Publicclass Checket extends Base {Checket () {Super ();//Call the constructor of the parent class, be sure to put it in the first statement of the methodSystem. out. println ("Checket");} PublicStatic
Java-based super and uper
I have briefly explained the basic inheritance usage. Now I want to add something that is easily overlooked in inheritance, such as super.
, I added an output statement to the constructor of the parent class (Father), but I did nothing in the Child class .. however, when the new subclass object is used, the content in the parent class constructor is output... why ??
Are you confu
Linux virtual file system four main objects:1) Super Block2) Index node (inode)3) Catalog entry (dentry)4) Document object (file)A process that refers to various objects as it operates on a file is as follows:by Task_struct get files_struct, then through the file descriptor (int fd) to obtain the corresponding file object (**FD), then get the Directory item object (Dentry), and finally get the Index node object (inode), The object includes actions rel
We already know that if a member variable defined in a subclass has the same name as a member variable in the parent class, the member variable in the parent class cannot be inherited, and the member variable of the child class is called the member variable of the parent class is hidden.When a method is defined in a subclass, and the method's name, return type, number of arguments, and a method of the parent class are exactly the same, this method of the parent class is hidden (rewritten), neith
The ability to implement super () and __init__ () is similar for single-inheritance
Class Base (object):d EF __init__ (self):p rint ' base create ' class Childa (base):d EF __init__ (self):p rint ' creat A ', base.__ Init__ (Self) class childb (base):d the EF __init__ (self):p rint ' creat B ', super (childb, self). __init__ () base = Base () A = Childa () b = childb ()
Output Result:
Base Createcreat A B
For example, classChild (Parent): def _ init _ (self): Parent. _ init _ (self), for example
class Child(Parent): def __init__(self): Parent.__init__(self) Reply content: the answer to your question is yes, and there is no difference. However, I don't feel good enough to answer this question.
To talk about super, we should first ignore the interference brought by the name "super.
Do not t
The difference between super () and _ init _ () in python classes is that python _ init __
The functions implemented by super () and _ init _ () for single inheritance are similar.
class Base(object):def __init__(self):print 'Base create'class childA(Base):def __init__(self):print 'creat A ',Base.__init__(self)class childB(Base):def __init__(self):print 'creat B ',super
this:First, call another constructor using this (parameter list). This is only used in the constructor of the class and cannot be used in other places.2. When the name of a function parameter or local variable in the function is the same as that of a member variable, the member variable is blocked. To access the member variable, you must use "this. to reference member variables. Of course, without the same name, you can directly use the name of the member variable instead of this. If you use th
Original address: https://www.cnblogs.com/hasse/p/5023392.htmlThese days to see the class in the inheritance will use this and super, here do a little summary, with you to communicate, there are errors please correct me ~ThisThis is an object of its own, representing the object itself, which can be understood as: A pointer to the object itself.The usage of this can be broadly divided into 3 types in Java:1. Normal Direct referencesThis does not have
The article turns from
@implementation Son:father
-(ID) init{
self = [super init];
if (self) {
} return
self;
}
This is an ordinary to initialization method that invokes the Init implementation of the parent class before the subclass implements initialization. Based on this initialization method, we compare the self and Super keywords from three aspects. what self is and what
Python: super
Getting started with super ()
In class inheritance, If you redefine a method, this method will overwrite the method of the same name as the parent class, but sometimes we want to implement the function of the parent class at the same time, we need to call the method of the parent class, which can be implemented by using super, for example:
Class Ani
------Super keyword------Super Purpose: Access superclass "hidden member variables (whether static or not) and static methods" and "overridden instance methods" in subclasses. The superclass here must be a "direct superclass", the most recent superclass above the subclass.How to use Super:① calls the constructor of the superclass in the subclass construction meth
1. Subclass constructors If you want to refer to super, you must put super in the first place of the function. class Base {base () {System.out.println ("base"); }} public class Checket extends Base {checket () {super ();//Call the constructor of the parent class, be sure to place the first statement of the method System.out.println ("Che Cket "); } public s
1. Constructor of subclass if you want to refer to super, you must put super at the top of the function
Copy Code code as follows:
Class Base {
Base () {
System.out.println ("Base");
}
}
public class Checket extends Base {
Checket () {
Super ();//Call the Parent class construction method, be sure to place the first statement
This is an object of its own, representing the object itself, which can be understood as: A pointer to the object itself.The usage of this can be broadly divided into 3 types in Java:1. Normal Direct referencesThis does not have to be said, this is equivalent to pointing to the current object itself.2. The name of the member who participates in the membership is distinguished by this:Class Person { private int: age = ten; Public person () { System.out.println ("Initialization Age:" +ag
Ext: 52205961The Python language has similar class inheritance to C + +, in which Python customizes the first self, like the this pointer in C + +, to the object itself.Examples of simple Python classes:[Python]View PlainCopy
>>> class Hello (object):
... def print_c ():
...print"Hello world!"
>>> hello (). Print_c ()
Hello world!
Of course, in practice, the inevitable need for class inheritance, subclasses inherit the parent class, normal as follows:[Python]View
The Python language has similar class inheritance to C + +, and in the class definition, Python customizes the first self, similar to the this pointer in C + +, pointing to the object itself.
Examples of Python simple classes:
>>> class Hello (object):
... Def print_c ():
. print "Hello world!"
>>> hello (). Print_c ()
Hello world!
Of course, in practice, the inevitable need for class inheritance, subclass inherits the parent class, normal as follows:
>>> class Child (Hell
.The non-parametric null constructor that the system automatically adds in the parent class is explicitly written out (in order to have the output, add println):[Java]View Plaincopy
Class base{
Base () {
System.out.println ("base constructor");
}
}
Class derived extends base{
public static void Main (string[] args) {
Derived d=new derived ();
}
}
Output: base constructor, code compiled. From the example, it seems that subclasses can inherit t
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.