[Thiinking in Java] autistic way of learning (a) constructor

Foreword -

 

This article is to learn to sort out the nature of the personal notes do, certainly with a lot wrong with it, for information purposes only.

(Chapter V-initialize and clean up the "Thinking in Java")

        Ensure initialized with constructors

text-

 

Constructor:

  What is the constructor? : C ++ introduced the concept of a constructor, which is a special method when you create an object called automatically, as well as in Java, by providing a constructor, to ensure that each object is initialized.

  Why constructor? : Use constructor helps reduce errors and make the code easier to read.

  Constructors look like? : Configuration uses the same name as the name of the class, the two must be identical, so the "method for each initial lowercase" coding style is not suitable builder.

  

  Does not accept any of the constructor parameter called "default constructor", the term commonly used in Java called "non-argument constructor"

 

  p.s:

    1. Conceptually, "initialization" / "create" independent of each other, but in Java code you can not find a clear call to initialize () of. In Java, "initialization" / "create" bundled together, the two can not be separated.

    2. The constructor is a special method, because it does not return a value (which is void like), but the return value is empty (void) Although the method itself does not automatically return anything, but you can still let him return something . Constructor there will not be any return value.

    3 · new expression does return a reference to the new object, but the structure itself and does not return value (if there is a return value, and that people can freely choose the type of return, it is bound deranged compiler knows how to deal with this type).      

    4. Although there is no explicit use of static keywords, but in fact the constructor is a static method (which will be mentioned in the initialization sequence)

public class Lianxi1_2 {
    public static void main(String[] args) {
        System.out.println("Noteone:"+new Noteone().note);
        Notetwo nt=new Notetwo();
        System.out.println("Notetwo:"+nt.note1+"||"+nt.note2);
    }
}

class Noteone{
    String note;
}

class Notetwo{
    String note1="hello";
    String note2;
    public Notetwo() {
        // TODO 自动生成的构造函数存根
        note2="hello in constructor";
    }
}
Exercise 1 and 2

 

 

Method overloading:

  Most programming languages ​​(especially C) is required for each process (also known as function) provides a unique identifier.

  In Java (and C ++), because the constructor's name has been determined by the class name, method overloading is particularly important. A method for the same name and different methods which exist in the form of parameters, it is necessary to use overloaded methods.

  ※ formal parameters: defining a function name of the function the variable names in brackets behind called formal parameters (the parameter), i.e. parameter appears in the function definition.

  How to distinguish between overloaded:

    1. The method of the same name but different parameters (or even the order of the parameters can be distinguished, but this is not recommended).

    2. Since the basic types can be "small" from a type of automatic transformation into a "larger" type, this process when it comes to heavy-duty, there will be some confusion.

      If the incoming data type ※ (argument / actual parameter types) less than the formal parameter declared method, actual data type will be lifted. "Char" type is slightly different, she would rise directly to the "int" type.

      If the incoming data type ※ large narrowing conversion must be performed (or other cast conversion method) by type of conversion.

    3. Remember! ! ! It can not be distinguished by the return value of the method type overloaded.

      E.g:

        void f(){ };

        int f(){return 1};

     In the "int i = f ();" indeed can be distinguished, but sometimes you do not care about the return value, call the method like this: "f ();" this time we can not distinguish which method is called.

 

The system will help you create a default constructor

  The default constructor (also known as no-argument constructor), she created the role of a default object. If you do not write the class constructor, the compiler will automatically help you create a no-argument default constructor.

  ※ If you've written a constructor, the compiler will not help you create (if you just write a constructor with parameters, because the compiler will not help you create a default constructor, so you create when the object must also provide parameters unless you wrote a separate no-argument constructor).

 

public  class Lianxi3_4 {
     public  static  void main (String [] args) {
         // method TODO automatically generated stubs 
        System.out.println ( new new Nothing () S.);
         new new Nothingbut ();
         new new Nothingbut ( "you" ); 
    } 
} 

class Nothing { 
    String S = "Nothing" ;
     // verification system creates a default constructor 
} 

class nothingbut {
     public nothingbut () {
         // the TODO automatically generated constructor stub 
        System.out.println ( "nothingbut constructor" ); 
    }
    public Nothingbut(String s) {
        System.out.println("nothingbut constructor with "+s);
    }
}
Exercise 3 and 4
public class Lainxi5_6 {
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Dog dd=new Dog();
        dd.barking(" ");
        dd.howling(12345);
        dd.barkAndHowl(123," ");
        dd.barkAndHowl(" ", 122);
    }

}
class Dog{
    void barking(String s) {
        System.out.println("dog is barking!");        
    }
    
    void howling(int i) {
        System.out.println("dog is howling!");
    }
    
    void barkAndHowl(String s,int i) {
        System.out.println("dog is barking and howling!");
    }
    void barkAndHowl(int i,String s) {
        System.out.println("dog is howling and barking!");
    }
}
Exercise 5 · 6 and 7

 

 

Extras: overloading, Reconstruction and rewrite (triple)

  Reconstruction: By adjusting the program code to improve software quality, performance, architecture and design patterns make the program more reasonable. Improve the scalability and maintainability of the software. Popular point that reconstruction is that when you find the inadequacies of the code, to change its structure, to optimize it.

  • Specifically reconstituted: can rewrite or reload the like, but is not limited thereto, and change the comment, function name class name changes, etc., can be called reconstructed;
  • Reconstruction purpose: to make the software easier to maintain, modify the Bug and the like.

 

  Rewriting: subclasses to override a method implemented by @override. In the parent class declares a vague way, does not give details of behavior, in subclasses by overriding parent class (the same method name, parameters, return values, etc.) in this way, let this examples of methods, suitable to subclass.

  • override (Overwrite): base class is a subclass of coverage.
  • Rewrite features:

    1, must be in succession, the

    2, method name, return type, and the number of parameters must be the same type of parameters

    3, the derived class overrides access method does not drop below the base class permissions

    4, the derived class exceptions thrown base class should be equal or less than the base class

 

Overload: parameter identification method is different functions. For example, you have a AMethod () with no arguments, then you use an additional method of the same name, with a parameter AMethod (String para), this process is called overloading.

 

Rewriting and overloaded difference:

  1. Rewrite must inherit, not overloaded.

  2. The override method name, the same number of parameters, parameter types are compatible, the same overloaded method name, a list of different parameters.

  3. The method of rewriting modifiers is greater than equal to the parent, regardless of overloading and modifiers.

  4. When not override generally thrown exception is not thrown parent class can throw runtime exceptions.

to sum up:

  Rewrite: Is the method itself rewritten.

  Overloaded: the number of different parameters of the same function name.

  Reconstruction: Rewrite regarded as part of the reconstruction, but also includes optimization of the reconstruction program structure.

 

※ reconstruction will actually appear in the volume of software engineering paper, give me the whole Mongolia, and today saw overloading, think of her, hereby record.

                              Reference links: 1. rewrite, reconstruction, overloaded difference

                                   2.java, the reconstruction, overloading, rewrite

Guess you like

Origin www.cnblogs.com/YFEYI/p/12156779.html