Learning java from scratch (2) classes and objects, learning java objects from scratch
Object-oriented refers to dividing a problem transaction into various objects. The object is created not to complete a step, but to describe the behavior of a thing in the whole process of solving the problem.
A class is the abstraction of an object, and an object is a specific instance of a class.
1. Construct a class so that it has attributes and Methods
Public class Employee {// attribute, private attribute, and provides a method for it to access private String name; private int age; private double salary; private static final int NUMBER = 217; // constant private static void method1 () {System. out. println ("static method");} static {System. out. println ("static block");} // constructor public Employee (String name, int age, double salary) {this. name = name; this. age = age; this. salary = salary; System. out. println ("constructor");} public void raiseSalary (double multiple) {this. salary * = multiple;} public static void main (String [] args) {Employee. method1 ();}}View Code
Class content loading sequence:
1. When a VM loads a Java class for the first time, it initializes the static initialization block, static member variable, and static method.
2. A class instance is created only when the new method is called.
3. class instance creation process: Initialize according to the parent-child inheritance relationship. First, execute the initialization part of the parent class, and then construct the parent class; then execute the initialization block of the subclass inherited by this class, and finally the constructor of the subclass.
4. When a class instance is destroyed, the subclass part is destroyed first, and then the parent class part is destroyed.
2. Overload overloading
Multiple methods have different parameters with the same name. In fact, they can be used as multiple methods...
public String color(){ return "red"; }public String color(String color){ return color; }View Code
3. public, private, protect, and "default" scopes
Scope: current class, same package, Child class, other package
Public √
Protected √ ×
Friendly √ ××
Private √ ×××
4. Method Annotation
@ Param variable description; @ return description @ throws exception description @ author description @ version description @ deprecated outdated
5. class design skills
1. Data must be private
2. Data must be initialized.
3. Do not use too many basic types in the class.
4. Split too many functions in the class
5. The class name and method name should reflect their responsibilities
6. class inheritance
Public class Employee {// attribute, private attribute, and provides a method for it to access private String name; private int age; private double salary; private static final int NUMBER = 217; // constant private static void method1 () {System. out. println ("static method");} static {System. out. println ("static block");} // constructor public Employee (String name, int age, double salary) {this. name = name; this. age = age; this. salary = salary; System. out. println ("constructor");} public void raiseSalary (double multiple) {this. salary * = multiple;} class Manager extends Employee {public Manager (String name, int age, double salary) {super (name, age, salary );}}View Code
If the parent class does not have a constructor without parameters, the Child class must overwrite the constructor of the parent class. Subclass can override the parent class method or add a new method. It is equivalent to expanding the parent class to adapt to other environments.
A subclass can inherit only one parent class. It can be understood as a IS-A; The subclass is the parent class. Interface, which can be understood as like-. A can also be B. If a class is modified by final, it cannot be inherited. For example, String class.
7. abstract class
An abstract class is a class with an abstract method. It is used for inheritance. Abstract classes can contain specific methods.
abstract class Person{ public abstract void eat(); public void sleep(){ } }View Code