java object 1 (study notes HOW2J)

  • Overload: Which specific implementation method based on the number and type of parameters passed judgment upon the same method name but different parameter types call it
public void attack()
public void attack(Hero h1)
public void attack(Hero  h1 , Hero h2)
  • A variable number of parameters of the function definition
public  void Attack (Hero ... heros)
 // in the function, operation using arrays can be handled heros
  • Construction method

  1. Object Instantiation achieved by the construction methods, such as Hero h1 = new Hero (), where Hero () is the default constructor Hero class.

  2. When defining the class, the display is specified as Hero (string name) {...} when reference constructor; then the default constructor with no arguments will no longer exist, unless explicitly defined in the class.

  3. The method of construction can be overloaded and ordinary methods as

  • this keyword

  1. this keyword represents the current object.

     2. this object access object properties such as this.name this.age

  3. By this method may be configured such that this call (name)

  • Parameter passing

  1. The basic types of transmission parameters: the method does not modify the basic method of the type parameters except

  2. The transmission parameter class type: class referenced type is also known, refers to the definition of a reference type variable pointing to an object such as: Hero h1 = new Hero () method of the case can change the reference to an object transfer properties

  • Wrapped package

  1. The ratio of nearly called the plan into a class in the same package

   Hero, ADHero plan in a package called charactor (role) Item, Weapon plan in another package, called the property (props)

1 package charactor
2 public class Hero{
3   String name;
4   float   hp;
5   float   armor;          
6   int moveSpeed
7 }

   2. Direct use the same classes in a package, but with different packet classes, must import

import property.Weapon;
  • Access modifier private public protected package (not write)

  1. The relationship between class      itself with steamed buns classes with different class to other classes bags

       private visit     to inherit inherit access access

       Access package inherited            inherited             access          access

      Inheritance Inheritance protected access access          access

      public           access to inherit inherit access access

  How to select modifier, dependent on the " scope minimum criteria "

  • Attribute class class method (static static)

  Class attributes and methods for the class of all classes, all objects share a .

  • Singleton

  Introduction: LOL just a big dragon GiantDragon, only one, so the class can only be instantiated once.

  Example starving single mode: (in any case to create an instance)

package charactor;
public class GiantDragon{
    private GiantDragon(){
        
    }
    private static GiantDragon instance = new GiantDragon();
    
    public static GiantDragon getInstance(){
        return instance;
    }
}

  Lazy man singleton :( will produce only instance when calling getInstance)

package charactor;
public class GiantDragon{
    private GiantDragon(){
        
    }
    private static GiantDragon instance;
    public static GiantDragon getInstance(){
        if(null == instance){
            instance = new GiantDragon();
        }
        return instance;
    }
}

  Hungry man style is immediately loaded way, whether with this object will be loaded; lazy man is lazy loading of way, will be used only when the load.

  Look business needs, if allowed to have more full time on business start-up and initialization , on the use of a hungry man type, otherwise use the lazy man.

  Singleton three elements:

    1. Constructors privatization

    2.  Static attribute points to examples

    3. getInstance public static method returns the static properties of the second step

 

  • Enumerated type

  enum is a special class, easy to use enumerations define constants

public enum Season{
    SPRING,SUMMER,AUTUMN,WINTER
}

  With enhanced for loop can be convenient calendar will enumerate

public class Test{
    public static void main(String[] args){
        for(Season s: Season.values()){
            System.out.println(s);
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/semie/p/12298361.html