Anonymous classes and wrappers for Java classes

Source: Internet
Author: User
Tags wrappers

/* Anonymous object: No reference type variable points to an object called an anonymous object. Requirements:  Use the  java class to describe a student class. Things to note for anonymous objects:1.  we generally do not assign property values to anonymous objects because they are never available. 2.  two anonymous objects can never be the same object. Anonymous object benefits: Simplify writing. The object's heap memory space can be freed as soon as possible application scenarios for anonymous objects:1.  if an object needs to call a method one time, and after the method is called, the object is no longer used, and an anonymous object can be used. 2.  can call a function as an argument. *///Student Class class student{    int num; //No.     String  name; //name     public void study () {         SYSTEM.OUT.PRINTLN ("Study hard, prepare for what will be called Gao!");     }}class demo4{    public static void main ( String[] args)      {        //Create a Student object         //student s = new student ();         //new student () .name =  "Dog Doll";  //Anonymous Object           //system.out.println (Student (). name);  //null        system.out.println (new  Student ()  == new student ())  ;  //  "= ="   When used to reference a type variable, the memory address is compared. Determines whether two objects are the same object         //demand:  calls student study method.         student s = new student ();         s.study ();         new student (). Study ();     }}


package com.fish.object;/* three characteristics of object-oriented:1.  encapsulation 2.  inheritance 3.  polymorphism. Requirements:   use Java class to describe the members of Lily Net. Question:  sex have problem?? Root cause:  Because other people can manipulate the sex attribute directly. You can assign a direct value to the sex attribute. Encapsulation: Permission modifier: the permission modifier is the visible range of the control variable. public :   the public.  public modified member variables or methods can be accessed directly by anyone. private :  Private,  private-modified member variables or methods can only be accessed directly in this class. The encapsulated step:1.  uses the private adornment property that needs to be encapsulated. 2.  provides a common way to set or get the private member property.   Naming specification: Set property name (); Get property name ();  doubt:  package must provide a get or set method? Not necessarily,  on demand. Don't want someone else to modify or access it does not provide a GET or set method specification  :  all member properties (member variables) of a generic entity class are encapsulated in real-world development. Entity classes: Entity classes are used to describe a class of   things called entity classes. Tool classes (tool classes for arrays arrays): The benefits of encapsulation:1.  improve the security of your data. The 2.  is simple to operate.  3.  hides the implementation. */class member{    publicstring name; //name      privatestring sex; //Sex     publicint salary; //Salary          //defines a common method for setting the Sex property     public void setsex (String  s) {        if  (S.equals ("male") | | S.equals ("female")) { //Note:  If you compare the contents of two strings, do not use = = to compare,  using the Equals method. Comparison of basic data types can be used = =, such as character type             sex = s;         }else{             //By default is male             sex =   "Male";         }    }    // Defines a common way to get the sex attribute     public string getsex () {         return sex;    }    //Chat      Public void talk () {        system.out.println (name+ "very happy chat") ;     }}class demo5{public static void main (String[] args)  {     membEr m = new member ();     m.name= "Zhao";     m.setsex ("     m.salary  = 800;    system.out.println ("Name:" +  m.name+ "  Gender:" + m.getsex () + "  Salary:" + m.salary);}


package com.fish.object;/* Requirements:  Use the Java class to describe a calculator class, the calculator has the operand 1,  operand 2 , the operator three public   properties, but also has the computational function behavior.   requires that:  not be directly assigned to the operand 1, operand 2, operator of these properties for direct  , to be encapsulated.   (+ - * /  ) provides a get or set method as required. You need to provide the Set method *///Calculator class class calculator{private int num1; //operand 1private int num2;   //operand 2 privatechar option ; //operator//provides a common method for setting property values ....public void  Initcalculator (Int n1 , int n2 , char o) {num1 = n1;num2 =  n2;if (o== ' + ' | | o== '-' | | o== ' * ' | | o== '/') {Option = o;} else{option =  ' + ';}} The computed function public void calculate () {switch (option) {case  ' + ': System.out.println ("Do the addition operation, the result is:" + (num1+ num2));break;case  '-': System.out.println ("Do subtraction, the result is:" + (NUM1-NUM2));break;case  ' * ': System.out.println ("Do the multiplication, the result is:" + (NUM1*NUM2));break;case  '/': System.out.println ("Do the division operation, the result is:" + (num1/num2)); Class demo6 {public static void main (String[] args)  {//created a Calculator object Calculator c = new calculator () ;//Set the property value C.initcalculator ("a");//Call Calculator's calculation function c.calculate ();}}


package com.fish.object;/* demand:  currently exists array: int[] arr = {0,0,12,1,0,4,6,0}  Write a function to receive the array , then clears the array of 0 and returns an array that does not exist with the 0 element. Step:1.  The length of the new array of computers.    original Array length-number of 0 */import java.util.*;class demo7{    public static  void main (String[] args) {        int[] arr =  {0,0,12,1,0,4,6,0};        arr = clearzero (arr);         system.out.println ("Elements of the array:" +arrays.tostring (arr));     }    public static  int[] clearzero (Int[] arr) {         //number of statistics 0         int count  = 0;//defines the number of a variable record 0         for (int i = 0  ; i<arr.length ; i++) {         if (arr[i]==0) {            count++;         }    }    //Create a new array     int[] newArr = new int[arr.length-count];     int index  =0 ; //the index value used by the new array     //stores the non-data in the new array.     for (int i = 0; i<arr.length ; i++) {         if (arr[i]!=0) {             newArr[index] = arr[i];             index++;        }    }     return newarr;    }}


This article from "Small Fish Blog" blog, declined reprint!

Anonymous classes and wrappers for Java classes

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.