In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "Java class, encapsulation, inheritance method", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's train of thought slowly in depth, together to study and learn "Java class, encapsulation, inheritance method"!
What is a class member
Member methods and member variables modified with static are called class members
Member variables modified with static are called class variables
Member methods modified with static are called class methods
What is an instance variable?
Member methods and member variables that are not modified by static are called instance members
Member variables that are not modified with static are called instance variables
Member methods that are not decorated with static are called instance methods
For example:
Public String name; / / this is the instance member (property) public static int age / / this is the class member (attribute) public void a () {System.out.println ("my name is:" + this.name)} / / the method without static is the instance method public static void a () {/ / the method with static is the class method System.out.println ("my name is: "+ this.name)} what's the difference between instance variables and class variables?
All objects of this class share the same class variable, but each object has its own unique instance variable.
All objects of this class can change the value of the class variable, but each object can only change its own instance variable value.
Before using the instance variable, you must create an object, according to the object name. Variable names are used, but class variables do not need to create objects
/ / define a class public class stu {public String name / / this class has a name, which belongs to the instance member, that is, you need to instantiate the object before you can use / / define the constructor public stu () {} / / anyway, define a no-parameter construction public stu (String name) {/ / define a parameterized construction, and you need to pass in a string name this.name = name / / if the this keyword is not used here, it will not match the above instance property} / / define the method public void a () {System.out.println (this.name);}}
Use this class:
Public class stu_Test {public static void main (String [] args) {/ / instantiate two objects based on the stu class stu S1 = new stu ("Xiao Hong"); stu S2 = new stu ("Xiao Wang"); / / use the method s1.a (); s2.a ();} / / stu S1 = new stu ("Xiao Hong") in the stu class Will output Xiao Hong / / stu S2 = new stu ("Xiao Wang"); will output Xiao Wang} / / through this you can understand that each object has its own instance variable (property) so the difference between class method and instance method?
All objects of this class share class and instance methods
The class method uses the class name. The method name ([parameter]) can be called without instantiating the object.
The instance method uses the object name. Method name ([parameter]) call
Static keyword
The Java class provides two types of variables: static variables modified with the static keyword and instance variables that are not modified by the static keyword. The static variable belongs to the class, and there is only one copy in memory. As long as the class in which the static variable resides is loaded, the static variable is allocated space, so it can be used. There are two ways to refer to static variables, namely, "class. Static variable" and "object. static variable"
Static member method:
The static method is the method of the class, which can be called without creating an object, while the non-static method is the method of the object, which can only be used after the object is created.
This and super keywords cannot be used in static methods, non-static methods cannot be called, and only static member variables and member methods of the class can be accessed, because when the static method is called, the object of this class may not have been created, and even if it has been created, it is impossible to determine which object's method to call.
The purpose of static:
Modify member variables
Modified member method
Static code block
Modifier class [can only modify inner class, that is, static inner class]
Static guide package
Static Note: static can only access static, non-static can access both non-static and static.
Encapsulation: the concept of encapsulation
Encapsulate objective things into abstract classes, and classes can only let trusted classes or objects operate their own properties and methods, and hide untrusted classes or objects. This process is called encapsulation. In short: seal off your information and show it to people you trust.
Classification of packages
Encapsulation of properties: set the property to private (private) to restrict its use only within the class
Encapsulation of methods: for method encapsulation, externally accessible methods are set to public and externally inaccessible methods are set to private
Use of encapsulation
Before wrapping, let's learn a new modifier: private private: restrict its use only inside the class (that is, methods and properties modified by private can only be found and used within this class, outside this class, this property cannot be found, which also achieves the effect of encapsulation)
/ / encapsulate private Strint name; / / for the attribute name, which we cannot find outside the class.
Now that it is encapsulated, there must be a way to modify and use the encapsulation. Yes, this is the get/set method.
Get/set method
Public class stu {private String name; public void setName (String name) {/ / set method: by calling this method by the object, you can modify the private property this.name = name;} public String getName () {/ / get method: if the object calls this method, you can use this method return name;}}
Use:
Public class stu_Test {public static void main (String [] args) {stu s = new stu (); s.setName ("Xiao Hong"); / / change the private attribute of name to Xiao Hong System.out.println (s.getName ());}
/ / after the program is run, the output value is Xiao Hong.
Inheritance: what is inheritance
1: a new class can be derived from an existing class, a process called inheritance
2: in the process of inheritance, the new class is called the subclass, the existing class is called the parent class, and the subclass will inherit the properties and behavior of the parent class.
Inheritance syntax:
Public class stu extends Student {/ / add extends to the class name and write the inherited parent class / / here you can write that the parent class does not issue attributes and methods public String ID; / / and so on.
Note: inheritance cannot inherit the private properties and methods of the parent class! As long as it is modified by private, it will not be inherited!
About subclasses: in addition to having properties and methods that are not private to the parent class, subclasses can also extend their own properties and methods
Use of inheritance:
Inheritance is a single inheritance, that is, a class can have only one parent class.
If a class does not explicitly inherit a class, then its default parent class is the java.lang.Object class
Inherits the non-private member variables and member methods of the parent class, but note that subclasses cannot inherit the constructors of the parent class
In short: a subclass can inherit only one parent class, and if this class does not inherit another class, it inherits the Object class by default (Java comes with it)
Cannot inherit the constructor of the parent class.
Method overrides:
@ overriding
What is method rewriting? :
The subclass is rewritten according to the method inherited from the parent class (the method name is the same)
Rewriting is a way to retain the father by using the super method (super will talk about it later)
Note: constructors cannot be overridden
Rules for method rewriting:
Same method name, same parameter list (quantity, order, data type)
If there is a return value, the return value is the same or its subclass, and the access permission cannot be stricter than the parent class.
The static method of the parent class cannot be rewritten as a non-static method, whereas the non-static method of the parent class cannot be rewritten as a static method.
A subclass can define a static method with the same name as the parent class to hide the static method of the parent class in the subclass * * (super, this cannot be used in static methods) * *
Private methods of a parent class cannot be overridden by a child class (private modified)
The above code:
1: define a class with attributes of name and age, with get/set methods, respectively, and member methods that output name and age:
Public class Person {private String name; private int age; / / get/ste method public void setName (String name) {this.name = name;} public String getName () {return name;} public void setAge (int age) {this.age = age;} public int getAge () {return age } / / member method: public void print () {System.out.println ("my name is:" + this.name+ "," + "my year:" + this.age+ ");}}
2: write a class that inherits the Person class, and the class has its own sex property, provides the get/set method and overrides the print method of the parent class, outputting name + age + gender
Public class child extends Person {private String sex; / / this child class inherits the Person class, but also has its own attribute sex gender public void setSex (String sex) {this.sex = sex;} public String getSex () {return sex } @ Override / / override the parent method: because the parent class outputs the name and age, here you need to output the name, age and sex public void print () {System.out.println ("my name is:" + getName () + "," + "my year:" + getAge () + "year +", "+" I am "+ sex+" child ");}}
3: create a new test class to test the inheritance and rewriting of the two classes
/ / Test class, public class Test {public static void main (String [] args) {child c = new child (); c.setName ("Xiao Hong"); c.setAge (20); c.setSex ("male"); c.print ();}} / / execute the set method of child inheriting person respectively, using the overridden method
/ / the output result is: my name is Xiao Hong, I am 20 years old and I am a boy
Super keyword:
Super represents the parent object
How to use super:
1:super. The attribute name is used to call an instance variable of the same name that the parent class is hidden in the subclass.
2:super ([parameter list]) is used to call the constructor of the parent class in the constructor of the subclass
Note:
When the constructor of each subclass does not show the call super (), the system provides a default super ().
Super () is written on the first line
You can display the call super () in the subclass constructor to complete the call to the specific parent constructor.
In short: super calls the parent's properties and methods to use the
The above code:
1: create a new class: define age as 20
Public class super_test {public int age=20; public void print () {System.out.println (this.age);}}
2: build a second class and inherit the first class
Public class su2 extends super_test {public int age = 10; @ Override public void print () {System.out.println (super.age); / / use super here, meaning to use the age}} of the parent class
3: create a test class:
Public class test {public static void main (String [] args) {su2 s = new su2 (); s.print ();}}
So the output is 20, which is the age of the parent class
The difference between this and super:
Super: it refers to the member in the direct parent class of the current object (used to access the hidden member data or function in the parent class in the direct parent class, such as: super. Variable name super. Member function data name (argument)
This: it represents the current object name (where ambiguity occurs easily in the program, this should be used to indicate the current object; if the member data in the function's shape participation class has the same name, this should be used to indicate the member variable name.
Super () is similar to this (), except that super () calls the constructor of the parent class in the subclass, and this () calls the constructor of the class within this class.
Both super () and this () need to be placed in the first line of the constructor, and although you can call one constructor with this, you cannot call both
This and super cannot appear in the same constructor at the same time, because this must call other constructors, and other constructors must have super statements, so if there is the same statement in the same constructor, the meaning of the statement will be lost and the compiler will not pass.
Both this () and super () refer to objects, so neither can be used in a static environment. Including: static variable, static method, static statement block.
In essence, this is a pointer to this object, while super is a Java keyword.
Thank you for your reading, the above is the content of "Java class, encapsulation, inheritance method". After the study of this article, I believe you have a deeper understanding of the problem of Java class, encapsulation, inheritance method, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.