Home > Java > javaTutorial > body text

Regaining the basics of Java (7): Summary of anonymous objects

黄舟
Release: 2017-01-16 09:34:41
Original
1606 people have browsed it

Regain the basics of java (7): Summary of anonymous objects

1. Anonymous objects

1. Anonymous objects refer to not giving names to objects ,        

For example:

一般对象为  
类名(例如Phone)对象名(自己起)=new 类名();            
则匿名对象为 new 类名();
Copy after login

2, Anonymous object

Every time it is used, it is equal to new a new object, so when only one method in an object is used, anonymous can be used Object

3. The advantage of anonymous objects is passing parameters;

  class WeiXin    
  //定义WeiXin类            
  {                 
  private  String wengben;                 
  //属性省略                 
  public void showSend(){                
   }             
   }            
   class Phone    
   //定义Phone类            
   {                 
   //属性省略                 
   public void showCall(WeiXin w){                 
   }             
   }             
   class TestPhone //测试类             
   {                 
   public static void main(String[] ages){                       
   Phone p=new Phone();                       
   p.showCall(new WeiXin("45135135135"));             
   }
Copy after login

4. It is easy to make mistakes;

When using anonymous objects, use get when calling private properties. Function; not directly p. Properties; private functions cannot be directly called; call

# in the middle of the public function in its category to call

#1. If you encounter two custom classes that need to call functions between each other; pay attention to passing parameters in the test class. In this way, you can avoid that after the attributes and other values ​​are assigned, the value can be used again when the object is used again. Revise.

2. Considerations about classes

     a、找名词    //作为类名      
     b、找名词     //个人认为是针对类  给类找的修饰语   作为属性之用      
     c、动作      //动作是用来创建函数的      
     4、找主动方和被动方   //  主动方来调用被动方
Copy after login

3. Easy mistakes

A class can have no action after searching for attributes, but after calling its attributes Be sure to identify whether private encapsulation is being carried out. If the encapsulation cannot use p. attribute

3. static keyword

1. static means static, but the static here is not static. means that when the current class where your static is located is loaded, space will be opened up in the method area together with the class; so the class will be there. This is static 2. Static members exist because the class already exists, so they can be called without creating an object. The method is class name.static attribute name class name.static method name (); 3. Static members can call static members directly ; If you want to call other members, you need to use an object. That is to say, the object can call all members. 4. Other members will exist after the object is created. 5. Those that exist first cannot call those that exist later, and those that exist later can call those that exist first. 6. Benefits Convenience Disadvantages Memory usage Limited use 7. Generally used in tool classes 8. Easy to make mistakes Static objects can be shared by all objects in their own class That is to say, if you create a new object and use static members, the following The content of this static member of a new object is not empty; For example:

Regaining the basics of Java (7): Summary of anonymous objects

##4. Code block

1. Code block That is The part enclosed by {} a. Constructed code block { Execution code } is placed in the class, similar to the constructor b. Static code block static{Execution code} is also placed in the class. The static class exists and is called only once. That is to say, the static is there, and if you create a new object, it will no longer call it. c. Local code block {execution code} is placed in the method. When the method is called, it will run immediately. 2. Running sequence Static code block > Construction code block > Construction Function > Local code block                                                   (                                                                               use. ##6. Math class


Mathematical methods that belong to the lang package can check the API table. But remember that everything that belongs to the lang package can directly use math. method name because lang is automatically loaded.

7. Scanner Class

1、需要导入包     import  java.util.Scanner  
2、键盘录入     
3、nextInt(),接收整数     
nextDouble(),接收小数     
next(),接收字符串    
 nextLine(),接收字符串   
 next() 接收时有效字符串前的所有 空格 、制表符都不记,他是从有效字段开始遇                
 到空格、制表符、回车就结束。  
  nextLine() 是接收文本的 从第一个任意字符开始   一旦遇到回车就结束    
  总结  
   一旦在nextLine() 前面有任意的键盘录入语句时都会在  栈 内留下一个          
   回车,nextLine()遇到直接结束    
   例如    
   Scanner sc=new Scanner(System.in);      
   System.out.print("请输入你的编号:");      
   int id=sc.nextInt();      
   System.out.print("请输入你的姓名:"); //虽然有输出语句但是此语句在方法区不影                                                      
   响栈    
   String name=sc.nextLine();  //其会在栈内接收一个回车  就等于输入了 空(什么                             
   都没有  直接进行下一语句)    
   System.out.println("你的编号是:"+id+",姓名是:"+name);
Copy after login

8. Random class

     1. java.util包       
     2. 创建对象    Random.方法名       
     3. 功能方法         
     nextDouble(),得到一个大于等于 0.0 且小于 1.0的随机小数         
     nextInt(),得到一个随机生成的int数字         
     nextInt(int n),得到一个大于等于0且小于n的随机int数字
Copy after login

The above is the content of regaining the basics of java (7): summary of anonymous objects. For more related content, please pay attention to the PHP Chinese website (www.php.cn )!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template