Java Basics Summary (1)

Source: Internet
Author: User

Definition classes: Access Modifiers class class name {}  access modifier such as: Public, priate is optional class is the keyword that declares the class according to the naming convention, class name capitalization   Example: Create "People" class, Key code: public class person{}********************************************************** Definition Properties: Access modifier data Type property name;  access modifier optional except access modifier, Other syntax and declaration variables are consistent   example: Create a "person" class, add the corresponding property named "People" Class, Key code: public class Person{public string name;public string gender;public int Age;}  ********************************************************** Definition method: The access modifier returns the type method name (argument type parameter name 1, argument type parameter Name 2, ...). The {}  access modifier is optional, the return type can be void, when the method is defined, when the return type is void, indicating that there is no return value, the method body does not have to use the "return" keyword to return the specific data, but you can use the "return" keyword to exit the method return type is not " Void ", then be sure to use the" return "keyword in the method body to return the result of the corresponding type, otherwise the program will have a compilation error in the parentheses" parameter type argument name 1, parameter type argument Name 2, ... "is called a parameter list when you need to pass parameters to the method when the method executes, If you do not need to pass the parameters can be omitted, but the parentheses can not be omitted, when passing multiple parameters, separated by the half-width of parentheses   Example: Define a method in the "person" class, describe the behavior of the person's work, Key code: public class Person{public String Name;public String gender;public int age;public void work () {System.out.println (this.name+ "Working philosophy: Striving for a Dream);}}  ********************************************************** Creating and Using objects: 1, creating object class Name Object name = new class name ();  nEW is the class name () to the right of the class name of the object to the left of the keyword () is called the constructor of the class   example: Creating an object of the person class, key code: Person Lilei = new Person (),   2, using object object name. Property object name of the attribute             //reference object. Method name         //method of referencing Object   Example: Object property assignment and calling method public static void main (string[] args) {Person lilei = new person (); lilei.name = "Li lei"; lilei.gender = "male"; lilei.age = 22;lilei.work ();}  ********************************************************** member Method: 1, parameter-with method   Create work () method the parameters defined are called formal parameters, referred to as formal arguments. The arguments passed in when the method is called are called actual arguments, referred to as arguments.  2, method overloading: The method name is the same, the parameter list is different, the method overload   feature is formed: In the same class, the method name is the same, the number of parameters or the type is different ******************************************** Member variables: 1, member variables: properties in a class, variables defined directly in the class, defined outside of the method. PS: A member variable can be assigned an initial value at the time of declaration, if it is not assigned, Java gives it a default value, the value of the base data type is 0, the value of the reference type is NULL2, the local variable: the variable defined in the method, in general, it needs to be assigned before use, otherwise it compiles an error *********** Construction method: The primary function is to perform some data initialization of the access modifier method name (parameter list) {method Body}  Constructor method does not return a value the default constructor method has no parameters, so the parameter list is optional (the default constructor for the parameterless method: alt+/) The method name of the constructor method is the same as the class name if you write a constructor with a parameter, you must generate an argument-freeConstruction methods   Example: Define a construction method for the person class, key code: public class Person{public person () {this.name = "Li Lei";}}   In a class you can define multiple overloaded construction methods The  this keyword is the default reference to an object, and within each instance method, there is a this reference variable that points to the object that called the method because this is a reference to itself within the object. So this can only invoke instance variables, instance methods, and construction methods. This cannot call a class variable and a class method, and this cannot call a local variable. 1) Use this to call the member variable to resolve the conflicting name of the member variable and the local variable. 2) Use this to call member Method 3) to invoke the overloaded constructor method, which can only be used in the constructor method, and must be the first statement of the constructor: public Penguin (String name,string sex) {this.name = name; this.sex = sex;} Public Penguin (string name, int health, int. Love, String sex) {this (name,sex); this.health = Health;this.love = Love;}  ********************************************************** Package Steps: Encapsulation: The use of OOP to complete the declaration of variables, assignment, using the completion of 2 times (Declaration, assignment, use): 1, Encapsulates a class declaration assignment using 2, the declaration assignment of a property inside a wrapper class using  1, modifying the visibility of a property to modify the property in the person class from public to private.   Example: Please privatize the properties of the person class, key code: public class Person{private string name;private string gender;private int age;}  2, setting Setter/getter () method   Example: Add the Setter/getter () method to the private property in the person class, key code: public class Privateperson{private String Name;private string gender;private int age;public string GetnaMe () {return name;} public void SetName (String name) {this.name = name;} Public String Getgender () {return gender;} public void Setgender (String gender) {This.gender = gender;} public int getage () {return age;} public void Setage (int.) {this.age = age;}}  3, setting access restrictions for attributes at this point, the properties are still not restrictive, and further use of conditional judgment statements to restrict the cases see MyEclipse-->practise-->setperson **************** Definition Packages: Package name  package is the declaration of the keyword must be the first non-annotative statement in the Java source file, And a source file can have only one package declaration statement, the package needs to correspond to the file system structure. Therefore, when you name a package, you adhere to the following coding specifications: A, a unique package name prefix is usually all lowercase ascii letters, and is a top-level domain, COM, edu, gov, net, and org, usually using the reverse order of the organization's network domain name. For example, if the domain name is javagroup.net, you can declare that the package name "Pacage net.javagroup.mypackage" B, the next part of the package name differs depending on the internal specifications of the different organizations. Such naming conventions may differentiate departments, projects, machines, or registered names by the composition of a particular directory name, such as "Package Net.javagroup.research.powerproject"  ************************** Package use: Package from large to lowercase: url upside down 1, the meaning of the declaration package is to declare where the current class is located 2, the meaning of the import package is declared in the current class to use the other classes in place  // The person class is put into the Pack1 package Cn.bdqn.pack1;public class person{... Omitting code}//the Persontest class into the Pack2 package//using the person class, you need to use import toImport Package Cn.bdqn.pack2;import Cn.bdqn.pack1.person;public class Persontest{public static void Main (string[] atgs) {...     Omit code}} ********************************************************** access modifiers for class members: scope: The same package in the same class anywhere in a subclass private Can not be not can not be the default modifier   can not be protected can not be able to   cannot be public  &n bsp;  can be used to **********************************************************static a modifier property of a keyword: Static modified property is called Or class variables, properties that do not use static modifiers are called instance variables.   Example: Leave the name, gender, and age properties of the person class, create a new static decorated property, and invoke the key code: public Person{public string Name;public string gender ;p ublic static int age;public static int person_live;//... Omit code}//above for the person class code, the following is the calling code//... Omit code person hanbing = new person (); hanbing.name = "hanbing"; hanbing.gender = "female"; hanbing.age = 22; Person.person_live = 1;  The most common scenario for a static keyword-decorated property in real-world development is to define constants that are decorated with the final keyword. Using the final keyword modifier constants cannot be changed while the entire program is running, and does not relate to specific objects, so use static adornments, such as "Static final  int person_live=1"  PS:  1, The constant name is generally capitalizedThe letter composition 2, the declaration constant must assign the initial value  ********************************************************** Modification method of the Static keyword: The method of static modification is called an instance method or a method of a class that is not modified by the static keyword.   Example: the ShowDetails () method in person is decorated with the static keyword and called, the key code: public person{public string Name;public string gender;public static int age;public static void ShowDetails (String name,string gender,int age) {System.out.println ("name is:" +name+ ", Gender: "+gender+", Age is: "+age",}}//above is the person class code, the following is the calling code public class Persontest{public static void Main (string[] args) {person Liudun = new Person (); Person.showdetails ("Lewton", "male", 23);}}  1, instance variables and instance methods are not directly accessible in static methods. 2, in the instance method can call directly the static variable and static method defined in the class **********************************************************4 basic behavior: 1) access modifier: Public Private Protected default 2) return type: The variable's definition data type variable name is not: void3) method Name: Similar to class name specification A, method name lowercase B, method name consists of multiple words, starting with the second word, the first letter capital C, Method name to make sense 4) parameter: There is: the declaration part of the variable (Boolean flag) does not: () 5) method body: Scope {}a, side by side: multiple methods can be parallel B, nested: method cannot be nested; can nest Process Control {if () {}} First basic behavior: Access modifier no return type Method Name (not) {} example: public void Add () {} The second basic behavior: the access modifier has no return type method name (with) {} example: public void Add (int num1,int num2) {}1, variable declaration process: Parameter form parameters 2, variable assignment of the process: the actual argument of the third basic behavior: the access modifier has a return type method name (NO) {} example: public int Add () {}1, keyword: Return is written after the variable name, The return type position is written with the data type of the variable 2, and the return cannot be followed by the fourth basic behavior: the access modifier has a return type method name (yes) {} The second and the third are combined in an example: public int Add (int num1,int num2) {}  Inherited syntax: Access modifier class <SubClass> extends < superclass>{}  in Java, inheritance is implemented through the extends keyword, where subclass is called a subclass, and superclass is called a parent class or base class. If the access modifier is public, the class is visible throughout the project. If the access modifier is not written, the class is only visible in the current package.   in Java, subclasses can inherit the following from the parent class: properties and methods that can inherit public and protected adornments, regardless of whether the subclass and parent class are in the same package can inherit the property and method of the default access modifier adornment. However, the subclass and parent must not inherit the parent class's constructor in the same package  ********************************************************** The syntax used by super: access to the Parent class construction method: Super (parameter) access to parent class properties/Methods:super.< parent class Properties/Methods > super can only appear in subclasses (methods and construction methods of subclasses). Instead of other locations, super is used to access the members of the parent class, such as the properties, methods, and construction methods of the parent class. Restrictions that have access, such as the inability to instantiate subclass objects through Super Access private**********************************************************: in Java, The construction method of a class is always performed in two cases: 1, the object that created the class (instantiation) 2, the object that created the subclass of the class (instantiation of the subclass) Therefore, when the subclass is instantiated, it first executes the constructor of its parent class before it executes the subclass's construction method.   When a subclass inherits the parent class, the calling rule of the constructor method is as follows: 1, if the subclassThe constructor method of the parent class is not invoked by the super display, and no other constructor method is called by the This display, and the parent class's parameterless constructor is called first. In this case, whether to write the super () statement, the effect is the same. 2. If the constructor method of the child class is called by the super display to call the parent class, then the corresponding construction method of the parent class will be executed instead of the parent class without the parameter construction method. 3, if the subclass of the constructor method through this display call itself other construction methods, in the corresponding construction method to apply the above two rules if there is a multilevel inheritance relationship, when creating a subclass object, the above rules will be applied to a higher level of the parent class more than once. Until you execute the non-parametric construction method of the top-level parent class object class. Object class: The object class is the parent class for all classes. In Java, all Java classes inherit the Java.lang.Object class   key code directly or indirectly: public class person{}//two-way equivalent public class person extends object{ } **********************************************************                         Partial method methods of the object class Description ToString () returns information about the current object itself, returns equals by a String object () compares two objects for the same object, returns Trueclone () to generate a copy of the current object, and returns the Hashcode () Returns the hash code value of the object GetClass () Gets the class information that the current object belongs to, and returns a class object ********************************************************** Method overrides and method overloads differ:  comparison Item location method name parameter table return value access modifier method overrides subclass same same or its subclasses cannot be more restrictive than parent class method overloadsThe same class is not the same as unrelated ********************************************************** polymorphism: Pets pet, there are several sub-categories, Pet class defines the method of the Doctor Toh Ospital (), subclasses rewrite the Doctor method, the key code: Class Pet{public void Tohospital () {System.out.println ("pet Doctor!"). ")}}class Dog extends pet{public void Tohostipal () {System.out.println (" Bird Doctor ")}}public class Test{public static void main (string[] args) {Pet Pet;pet = new Dog ();p et.tohospital ();p et = new Bird ();p et.tohospital ();}} In the   example, the pet class is generally declared as an abstract method because its own instantiation has no meaning, and the Tohopital () method is declared as an abstract class.   Abstract class cannot be instantiated subclasses if it is not an abstract class, you must override all abstract methods in the abstract class the abstract modifier cannot be used in conjunction with the final modifier for an abstraction method without the method body the Private keyword does not decorate the abstract method * * * Abstract methods: Abstract methods without method body abstract methods must be implemented in the abstract class, unless the subclass is abstract class ********** The syntax for upward transformation: the transformation of a subclass to a parent class is called up transformation < parent type >< reference variable name > = new < subtype > ();  a reference to a parent class to a subclass object, called an upward transformation, which is automatically made for type conversion when a method called by a parent class reference variable is overridden or inherited by a method of the parent class, not a method of the parent class, which is not called by the parent class to invoke a method that is unique to the child class  ****** Downward-shifting syntax:< subtype >< reference variableVolume name > = (< subtype >) < reference variable of parent type >********************************************************** Instanceof operator: Used to determine whether an object belongs to a class in the process of downward transformation, if not converted to the real subclass type, there will be type conversion Exception   Example: Determine the pet type, key code: public class Test{public static void Main (string[] args) {Pet pet = new Bird ();p et.tohospital (); if (Pet instanceof dog) {Dog dog = (dog) pet;dog.catchingflyd ISC ();} else if (pet instanceof Bird) {Bird Biird = (Bird) pet;bird.fly ();}}}   When using instanceof, the type of the object must have a subordinate relationship with the class specified by the argument following instanceof, or a compilation error will occur. Polymorphic applications: 1. Use the parent class as the formal parameter of the method: a master class that defines the method of controlling the animal's bark in the class.   Key code: Class Host{public void Letcry (Animal Animal) {animal.cry ();}} public class Test{public static void Main (string[] args) {host host = new host; Animal animal;animal = new Dog (), Host.letcry (Animal); Animal = new Cat (); Host.letcry (Animal); Animal = new Duck (); Host.letcry (animal);}}  2, using the parent class as the return value of the method, uses the parent class as the return value of the method, and is the primary way to implement and use Polymorphism in Java: The host sends out three kinds of animals that can be called.   Key code: Class Host{public Animal donateanimal (String type) {AnimaL animal;if (type== "dog") {animal = new dog ();} else if (type== "cat") {animal = new Cat ();} Else{animal = new Duck ();} return animal;}} public class Test{public static void Main (string[] args) {host host = new host; Animal animal;animal = Host.donateanimal ("dog"); Animal.cry (); Animal = Host.donateanimal ("cat"); Animal.cry ();}}                                    

Summary of Java Fundamentals (1)

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.