Java overload, overlay, and constructor __ block chain

Source: Internet
Author: User
/** * Copy Constructors---copyf t2 = new Copyf (t1); The default constructor is not invoked.
   * Copy Clone and Reference * overloads are in the same class (scope), and overrides are subclasses for the parent class.
   Overload does not care about the return value type.
Static methods cannot be overwritten.  * Override can be translated as overlay, literally knowing that it is overriding a method and rewriting it in order to achieve different roles. The most familiar overlay for us is the implementation of the interface method, in which the method is generally declared, and we need to implement all the methods of the interface declaration when we implement it. In addition to this typical usage, we may also overwrite methods in the parent class in the inheritance. In the cover to note the following points: 1, the cover of the method of the logo must be covered with the method of the logo exactly match, to reach the effect of coverage; 2. The return value of the overridden method must be consistent with the return of the overridden method; 3, the exception thrown by the overridden method must be the same as the exception thrown by the overridden method.
	or its subclasses; 4, the overridden method cannot be private, otherwise only a new method is defined in its subclasses, and it is not overwritten. Overload may be more familiar to us, can be translated as overloading, it means that we can define some of the same name methods, by defining different input parameters to differentiate these methods, and then call, the VM will be based on different parameter styles, to select the appropriate method to execute. The following are some points to note when using overloads: 1. You can only use different parameter styles when using overloads. For example, different parameter types, different number of parameters, different parameter order (of course, several parameter types within the same method must not be the same, for example, can be fun (int,float), but not fun (Int,int)), 2, not through access rights, return type,
	 
	 The exception thrown is overloaded; 3. The exception type and number of the method do not affect the overload; 4. For inheritance, if a method is priavte in the parent class, then it cannot be overloaded in subclasses, and if defined, it simply defines a new method and does not achieve the overload effect.
	Constructors cannot be inherited.
	The constructor of the parent class must be shown or implicitly called in the subclass constructor.
 Note: When the parent class displays a constructor that has a parameter defined, the compiler does not give it a default constructor that specifies a null argument to be invoked to the subclass.

* * */package t;
Import Java.util.Vector; Class copyf{Private String a = "Z";//belongs to part of the constructor, andExecution first-equivalent to writing in the constructor private String B;
	Public String Geta () {return A;
	} public void SetA (String a) {this.a = A;
	Public String Getb () {return b;
	public void Setb (String b) {this.b = b; ///Copy with constructor (no default constructor is executed), and copy the corresponding property as needed.
	For example, if you do not want to copy attribute A, the first sentence of the following is a = Source.a;
		Public Copyf (Copyf source) {//a = Source.a;
	b = source.b;
	}//Constructor public Copyf () {System.out.println ("default constructor");
	//Copy by method.
		Public Copyf Clone () {COPYF t = new Copyf ();
		T.a= A;
		t.b = b;
	return t; } public class mythreadprinter2{public static void Main (string[] args) throws Exception {Copyf T1 = new
		COPYF ();
		T1.seta ("a");
		COPYF t2 = new Copyf (t1);//copy constructor---not copied a copyf t3 = T1.clone ();//General Member Method System.out.println (T2.geta ());

		System.out.println (T3.geta ());
		Vector<string> va = new vector<string> ();
		Va.add ("123"); vector<string> va2 = va;//reference vector<string> va3 = (vector<string>) va.clone ();//copy-clone//va.get (0
		); Va.set(0, "234");
		System.out.println (va2.get (0));


	System.out.println (va3.get (0));  

 }   
}

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.