A variable length method when the number of function parameters passed into the call is indeterminate, in the methodThe last form of participationType followed by a three dot number (...). ), indicating that the shape is able to accept multiple participation values. Multiple values are passed in as arrays, and the parameters must be of the specified type.
Package wbb.java.javaobject;/** * Created with IntelliJ idea. * USER:WBB * date:14-7-17 * Time: PM 4:26 * To change this template with File | Settings | File Templates. */public class Mulparams {public static void Main (string[] args) { Mulfun ("basketball", "soccer"); } public static void Mulfun (String ... ballname) {for (string name:ballname) { System.out.println (name); } }}
Second, the method overload 1, the same class method name similarly, the method of the parameter type or the number of parameters is called the method overload, 2, the method's modifier, the return value is not the method overload; third, the use of Superkeyword Super Key Usage precautions: 1, Assume that member variables and methods in the parent class are defined as private types. Then the subclass will never be able to access them, assuming that the super.name is used to access the VAR variable of the private type of the parent class. Will cause a compilation error. 2, only in the construction method or strength method to use Superkeyword, and in static methods and static code block can not use Superkeyword.
Package wbb.java.javaobject;/** * Created with IntelliJ idea. * USER:WBB * date:14-7-21 * Time: Morning 9:31 * To change this template use File | Settings | File Templates. */public class OverWrite {public static void Main (string[] args) { b b = new B (); B.test (); }} Class A {public String name = "WBB"; public void Test () { System.out.println ("Parent class test Method");} } Class B extends A {public void Test () { System.out.println (super.name); Super.test (); }}
Output Result: WBBThe parent class test method, polymorphism 1, assumes the conversion of reference variables to subclass types is called downward transformation. 2, the assumption that the reference variable to the parent class type is called upward transformation; the ToString () method The ToString method provided by the object class always returns the class name +@+hashcode value of the implementation class of the objects, and this return value does not really implement the function of "self-descriptive narration". The ToString method of the object class must be overridden if the user wants to implement the "self-descriptive narrative" function.
Package wbb.java.javaobject;/** * Created with IntelliJ idea. * USER:WBB * date:14-7-23 * Time: Morning 9:31 * To change this template use File | Settings | File Templates. */public class ToString { public static void Main (string[] args) { Animal Animal = new Animal ("Loyal dog Eight Male"); SYSTEM.OUT.PRINTLN (animal); }} Class Animal { private String name; Public Animal (String name) { this.name = name; } Public String toString () {// return "The dog's name is" + name;// }}
The output is: [Email protected]Remove the gaze code rewrite the ToString method to achieve the object's self-descriptive narrative: The dog's name is loyal dog eight male six, Singleton class suppose a class is always created only once. Then call this Class A singleton class. Why use a singleton class, because some objects only need to create once to be able to create more wasteful system overhead, for example, in a small room in a air conditioning can play a very good cooling effect, assuming that two or more units are a bit wasted.
package wbb.java.javaobject;/** * Created with IntelliJ idea. * USER:WBB * date:14-7-25 * Time: The afternoon of the change of the "this" template use File | Settings | File Templates. */public class Singleton {//define a static variable to cache the created instance private static Singleton instance; To avoid other classes being free to create instances of this class. Declare the constructor as private, only within that class the ability to call the constructor private Singleton () {}//provide a public method as an access point for that class, and you must use the static adornment.
//The object does not exist until the method is called. Therefore, it is not possible to call this method an object, just a class. Because member variables are used in static methods, they are defined with static modifiers. public static Singleton getinstance () {if (instance = = null) {instance = new Singleton (); } return instance; }}class Testsingleton {public static void main (string[] args) {Singleton Instance1 = singleton.getinstance (); Singleton Instance2 = Singleton.getinstance (); The output is true, indicating that the two reference variables point to the same object System.out.println (Instance1 = = Instance2); }}
Java Learning Notes (iv) object-oriented