Dark horse programmer – Java object-oriented (this keyword, static, singleton design pattern, inheritance)

this keyword It seems that it is used to distinguish between local variables and member variables with the same name. Why can this solve this problem? What exactly does this represent? This represents an object of this class. Which one does it represent? this represents a reference to the object to which the function it belongs belongs. Simply put, which object is calling the function where this is located, this represents that object. Application of this: When defining a function in a class, when the object that calls the function is used inside the function, this is used to represent the object. In other words, whenever this class object is used internally in this class function, it is expressed by this. Calls between constructors can only use this statement. This statement can only be defined on the first line within the constructor. Requirement: Define a function for comparing whether the age is the same, that is, whether they are of the same age. Code example: class Person{private String name;private int age;Person(int age){this.age = age;}public boolean compare(Person p){return this.age == p.age;}}class PersonThis{public static void main(String[] args){Person p1 = new Person(22);Person p2 = new Person(25);boolean b = p1.compare(p2);System.out.println(b);}} static keyword Static meaning.…

Dark Horse Programmer—-Java Basics Day 7 (Abstract Class, Inheritance, Final, Interface)

——Java training, Android training, iOS training, .Net training, looking forward to communicating with you! ——- 7.1 Abstract class /* When the same function appears in multiple classes, but the functional subjects are different, This can be extracted upward. At this time, only the function definition is extracted, but not the function body. Abstract: Can’t understand. Characteristics of abstract classes: 1. Abstract methods must be in abstract classes. 2. Both abstract methods and abstract classes must be modified with the abstract keyword. 3. Abstract classes cannot use new to create objects. Because there is no point in calling abstract methods. 4. To use abstract methods in abstract classes, all abstract methods must be overridden by subclasses and then called by subclass objects. If a subclass only covers some abstract methods, then the subclass is still an abstract class. Abstract classes are not much different from general classes. You can describe things how you want to describe them, but there are some incomprehensible things about them. These uncertain parts, which are also the function of the thing, need to appear explicitly. But the subject cannot be defined. Represented through abstract methods. Abstract classes have more abstract functions than general classes. That is,…

Java object-oriented, inheritance, polymorphism

1, inheritance In Java, you can inherit a class through the extends keyword, and implements can inherit multiple interfaces. Inheritance allows the creation of hierarchical classes. The inheritance mechanism improves the reusability of code and creates relationships between classes. Only with this relationship can we have the characteristics of polymorphism. Note: Java supports single inheritance, but can have multiple levels of inheritance. Subclasses can have non-private methods and attributes of the parent class. Subclasses can inherit the methods of the parent class, override the methods of the parent class, and extend the parent class. Increases the connection between classes, that is, improves coupling. The general format is: class Parent class{ } class Subclassesextends Parent class{ } Demo1: 1 package com.hpioneer.Demo; 2 3 public class Test_Extends { 4 public static void main(String[] args) { 5 Zi z = new Zi(); 6 //z.show(); 7 } 8 } 9 class Fu { 10 static { 11 System.out.println(“static code block Fu” ); 12 } 13 14 { 15 System.out.println(“Construction code block Fu” ); 16 } 17 18 public Fu() { 19 System.out.println(“Construction method Fu”); 20 } 21 } 22 23 class Zi extends Fu { 24 static { 25 System.out.println(“static code block Zi” );…

Dark Horse Programmer – [Java Basics] – Object Oriented (1) Overview, Classes and Objects, Inheritance, Abstract Classes, Interfaces, Polymorphism, Internal Classes

———- android training, java training, looking forward to communicating with you! ———- 1. Overview of object-oriented 1. Object-oriented: It is a very abstract concept. Compared with process-oriented, it is a programming idea. 2. Object-oriented characteristics: * Thoughts that conform to people’s thinking habits * Can simplify complex things * Convert programmers from executors to commanders * When completing the requirements: You only need to find objects with the required functions or create objects with the required functions, simplifying development efficiency and improving object reusability. 3. Three characteristics of object-oriented: Encapsulation, Inheritance, and Polymorphism. 2. Classes and Objects (1) The relationship between classes and objects 1. Definition of class: A class is a description of the common characteristics and functions of a group of things. A class is an overall description of a group of things. Java uses classes to describe the properties and behavior of objects. 2. Object: Object is an instance of a class, 3. Class definition format: [Modifier] class class name{ Properties; Construction method; General methods;    } (2) The difference between member variables and local variables Member variables: * Member variables are defined in the class and can be accessed throughout the class. * Member variables are established…

Java basics – object-oriented (encapsulation, inheritance, polymorphism)

Java basics – object-oriented (encapsulation, inheritance, polymorphism)

 Learning againJ2SE, I have to lament that I The basic content of this part is not reliable, so I will summarize it here. The basic ideas of object-oriented will not be mentioned here. This article only focuses on the object-oriented in java Describe the basic content. Encapsulation (1)Attributes ①Variable (static attribute) Member variables can bejavaAny data type in the definition is not initializedjavaIt can be initialized by default, and the initialization value is: Type byte shot int long char float double boolean Quote Value 0 0 0 0L ‘\u0000’ 0.0F 0.0D false null Local variables within the corresponding method or statement block must be defined. Display initialization, otherwise the compilation will not pass. ②Method (dynamic attributes) A code fragment used to complete a specific function. a.Construction method Construct object A method that is executed when new() is called and can be used to initialize the members of the object Variable; javaDefault The constructor is empty; Subclass construction will first execute the construction method of the parent class object super(argument_list), and then execute the inherited class construction method. b.Method overloading In a classDefine multiple methods with the same name but different parameters (number, type); When called, different methods will be…

Java basics interview – abstract classes, interfaces, polymorphism, inheritance

Java basics interview – abstract classes, interfaces, polymorphism, inheritance

1. Abstract class , Interface 1. Abstract class 1. Abstract class is modified with abstract. It has the same data fields and method constructors as regular classes. , but new instances are not allowed; 2. There may not be an “abstract method” in the abstract class, and the “abstract method” must be in the “abstract class’; 3. “Not When inheriting an “abstract class”, an “abstract class” must cover/implement all “abstract methods”, otherwise it will violate rule 2; 4. abstract and final cannot be used together, otherwise subclasses cannot inherit; 5. Subclasses of abstract classes can be abstract classes, which eliminates the need to implement all abstract methods; 1. Animal class (abstract class)abstract class Animal{ private String name; public void setName(String name){ this.name=name; } public String getName(){ return this.name; } 8. There may not be an abstract method in an abstract class, the abstract method must be in the abstract class The abstract method in the parent class must be subclassed Override, otherwise the subclass will inherit the abstract method, resulting in an error public abstract void doSome(); abstract method! public abstract void eatSome(); abstract method 4. abstract and final cannot be used together br> Error: java: Illegal modifier combination: abstract…

In Java, what are encapsulation, inheritance, polymorphism and abstraction, their benefits and usage

The four major features of Java are encapsulation, inheritance, polymorphism and abstraction. 1. Encapsulation The concept of encapsulation: combine the properties and methods of an object into an independent whole, hide implementation details, and provide an interface for external access. Benefits of encapsulation: (1): Hide implementation details. It’s like if you buy a TV, you only need to know how to use it, but you don’t need to understand its implementation principles. (2): Security. For example, if you privatize the age attribute in your program and provide external get and set methods, when the outside world uses the set method When you cannot set a value for an attribute, you can make an if judgment in the set method and set the value between 0 and 80, so that he cannot be arbitrary Assigned a value. (3): Increase code reusability. It’s like various methods encapsulated in tool classes. You can call them repeatedly anywhere without having to implement the details everywhere. (4): Modularization. Encapsulation is divided into attribute encapsulation, method encapsulation, class encapsulation, plug-in encapsulation, module encapsulation, system encapsulation and so on. It is conducive to the division of labor of the program without interfering with each other. It facilitates…

java notes object-oriented, construction, inheritance

Three major characteristics of object-oriented: 1. Encapsulation Invisible to the outside world 2. Inheritance Functions of extended classes 3. Polymorphism Method overloading Polymorphism of objects Construction method: The constructor name must be consistent with the class name Constructor has no return value The constructor is mainly used to initialize the attributes in the class Pass by reference This Keywords 1. Represent the attributes and calling methods in the class; 2. Call the constructor of this class; 3. Indicates the current object; package test04; class people{private String name;private int age;private String sex;public people(String name,int age){this();//Call the constructor of this class; this.name = name;//Represent the attributes and calling methods in the class this.age = age;}public people(){sex = “man”;System.out.println(“smncjdksjdkjs”);}public String getName(){return name;}public void setName(String name){this.name = name;}public int getAge(){ return age;}public void setAge(int age){this.age = age;}public void tell(){System.out. println(“Name:”+this.getName()+”Age:”+this.getAge()+”Gender:”+sex);}}public class thistest { public static void main(String[] args){people p = new people(“Zhang San”, 23);p.tell();}} Static Keywords 1. Use staticDeclare attributes StaticDeclare global properties 2. Use staticDeclaration method Call directly through the class name 3. Note: When using the static method, you can only access static Properties and methods declared other than staticare not accessible. 4. Static static functions cannot call non-static functions (methods) and…

Java calculator class diagram_polymorphic calculator (encapsulation, inheritance, polymorphism, simple factory)

Java calculator class diagram_polymorphic calculator (encapsulation, inheritance, polymorphism, simple factory)

1. Encapsulation A very important technology in object programming is encapsulation, that is, encapsulating objective things into abstract classes, and classes You can only allow trusted classes or objects to operate your own data and methods – and hide information from untrusted ones. The advantage of this is that it can make the specific implementation inside the class transparent – as long as other codes do not rely on the private data inside the class – you can modify these codes with peace of mind. In addition,This is also done for security reasons,If the variable representing the online payment card password can be accessed casually,Who would dare to use such a system? Access modifier: Private: Only the class itself can access. Protected: The class and derived classes can access. Internal: Only classes in the same project can access. Protected Internal: It is a combination of Protected and Internal. Public: Full access. Example code class Operation //Operation base class {private double _numberA = 0;private double _numberB = 0;public doubleNumberA {get { return_numberA; }set { _numberA =value; } }public doubleNumberB {get { return_numberB; }set { _numberB =value ; } }public virtual double GetResult() //Define virtual method {double result = 0;returnresult; } }…

Three major characteristics of Java object-oriented: encapsulation, inheritance, polymorphism

1. Three major characteristics of object-oriented: encapsulation, inheritance, polymorphism 2. Packaging: 1. Encapsulation has two major characteristics: reasonably hiding data and reasonably exposing data. 2. Modify the data with private. You can expose the data by defining a public method. 3. Inheritance 1. Java is single inheritance, but it can be inherited indirectly, extends 2. In java, any class, except Object, has a parent class 3. What attributes and methods can a subclass inherit from the parent class? Under the same package: public, default, protected Under different packages: public, protected Visit: Under the same package: public, default, protected Under different packages: public 4. Override/override: In the inheritance relationship, the method name of the subclass is the same as the method name of the parent class, the parameter type is the same, the return type is the same, and the access permissions are the same Subclasses cannot be more strict than parent classes: private > default > protected > public @override 5. Overloading: In a class, the method name is the same, the parameter type is different, and there is no requirement for the return value. 6. The construction method cannot be inherited, but the construction method of the subclass will…

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索