JAVASE Foundation - day07 (Object Oriented)

Keywords: jvm Attribute Mobile Java

07.01_Object-Oriented (Overview and Format of Constructor Construction Method) (Master)

  • A: Summary of tectonic methods and their functions
    • Initialize data (attributes) of an object
  • B: Format Characteristics of Construction Method
    • a: Method name is the same as class name (size is the same as class name)
    • b: No return value type, not even void
    • c: There is no specific return value;

07.02_Object-Oriented (Overload of Construction Method and Notices) (Master)

  • A: Case demonstration
    • Overload of construction method
    • Overload: The method name is the same, independent of the type of return value (the constructor does not return a value), just look at the list of parameters
  • B: Notices for Construction Method
    • A: If we do not give a construction method, the system will automatically provide a parametric construction method.
    • b: If we give the construction method, the system will no longer provide the default parametric construction method.
      • Note: At this point, if we want to use the parametric construction method, we must give it ourselves. It is suggested that the parametric construction method should always be given by oneself.

07.03_Object-Oriented (Difference between Two Ways of Assigning Membership Variables)

  • A:setXxx() method
    • Modify attribute values
  • B: Construction Method
    • Initialization of attributes in objects

07.04_Object-Oriented (Student Class Code and Testing) (Mastery)

  • A: Case demonstration
    • Student class:
      • Membership variables:
        • nameļ¼Œage
      • Construction method:
        • No ginseng, with two ginseng
      • Membership methods:
        • getXxx()/setXxx()
        • show(): Output all member variable values for this class
  • B: Assign member variables:

    • a:setXxx() method
    • b: Construction method
  • C: How to output the values of member variables:

    • a: Get them separately by getXxx() and then stitch them together
    • b: By calling show() method.

07.05_Object-Oriented (Code and Test of Mobile Class) (Master)

  • A: Case demonstration
    • Imitate the student class and complete the code of mobile phone class
class Demo5_Phone {
    public static void main(String[] args) {
        Phone p1 = new Phone();
        p1.setBrand("Apple");
        p1.setPrice(1500);
        System.out.println(p1.getBrand() + "..." + p1.getPrice());

        Phone p2 = new Phone("millet",98);
        p2.show();
    }
}
/*
Mobile phone category:
    Membership variables:
        brand, price
    Construction method
        No ginseng, no ginseng
    Member method
        setXxx And getXxx
        show
*/
class Phone {
    private String brand;                       //brand
    private int price;                          //Price

    public Phone(){}                            //Empty parameter structure

    public Phone(String brand,int price) {      //Parametric structure
        this.brand = brand;
        this.price = price;
    }

    public void setBrand(String brand) {        //Set up brand
        this.brand = brand;
    }

    public String getBrand() {                  //Acquire brand
        return brand;
    }

    public void setPrice(int price) {           //Set price
        this.price = price;
    }

    public int getPrice() {                     //Get price
        return price;
    }

    public void show() {
        System.out.println(brand + "..." + price);
    }
}

07.06_Object-Oriented (Step to Create an Object) (Master)

  • A: Drawing demonstration
    • Drawing shows what an object does in its creation process?
    • Student s = new Student();
    • 1. Student. class loaded into memory
    • 2. Declare a Student type reference s
    • 3. Create objects in heap memory.
    • 4. Default initialization values for attributes in objects
    • 5. Display initialization of attributes
    • 6. Construct method stack, assign attribute value to object, construct method stack
    • 7, assign the address value of the object to s

07.07_Object-Oriented (Rectangular Case Exercise) (Mastery)

  • A: Case demonstration
    • Demand:
      • Define a rectangular class, define the method of calculating circumference and area.
      • Then a test class is defined for testing.
class Test1_Rectangle {                         //Rectangle Rectangle
    public static void main(String[] args) {
        Rectangle r = new Rectangle(10,20);
        System.out.println(r.getLength());      //Perimeter
        System.out.println(r.getArea());        //The measure of area
    }
}
/*
* A:Case demonstration
    * Demand:
        * Define a rectangular class, define the method of calculating circumference and area.
        * Then a test class is defined for testing.
    Analysis:
        Membership variables:
            Wide width, high height
        Spatial parameter and parametric structure
        Membership methods:
            setXxx And getXxx
            Circumference: getLength()
            Find area: getArea()
*/
class Rectangle {
    private int width;              //wide
    private int high;               //high

    public Rectangle(){}            //Empty parameter structure

    public Rectangle(int width,int high) {
        this.width = width;         //Parametric structure
        this.high = high;
    }

    public void setWidth(int width) {//Set width
        this.width = width;
    }

    public int getWidth() {         //Get wide
        return width;
    }

    public void setHigh(int high) { //Set high
        this.high = high;
    }

    public int getHigh() {          //Get high
        return high;
    }

    public int getLength() {        //Gain perimeter
        return 2 * (width + high);
    }

    public int getArea() {          //Acquisition area
        return width * high;
    }
}

07.08 Object-Oriented (Employee Case Exercise) (Mastery)

  • A: Case demonstration
    • Requirements: Define an Employee class
    • Analyse several members by yourself, and then give the member variables.
      • name, id, salary
    • The construction method,
      • Spatial and parametric
    • getXxx()setXxx() method,
    • And a way to display all member information. And test.
      • work
class Test2_Employee {                      //employee
    public static void main(String[] args) {
        Employee e = new Employee("Linghu Chong","9527",20000);
        e.work();
    }
}
/*
* A:Case demonstration
    * Requirements: Define an Employee class
    * Analyse several members by yourself, and then give the member variables.
        * name, id, salary 
    * The construction method,
        * Spatial and parametric
    * getXxx()setXxx()Method,
    * And a way to display all member information. And test.
        * work 
*/
class Employee {
    private String name;                    //Full name
    private String id;                      //Job number
    private double salary;                  //wages

    public Employee() {}                    //Empty parameter structure

    public Employee(String name, String id, double salary) {//Parametric structure
        this.name = name;
        this.id = id;
        this.salary = salary;
    }

    public void setName(String name) {      //Set name
        this.name = name;
    }

    public String getName() {               //Get name
        return name;
    }

    public void setId(String id) {          //Set id
        this.id = id;
    }

    public String getId() {                 //Get id
        return id;
    }

    public void setSalary(double salary) {  //Wage setting
        this.salary = salary;
    }

    public double getSalary() {             //Get wages
        return salary;
    }

    public void work() {
        System.out.println("My name is:" + name + ",My work number is ____________:" + id + ",My salary is:" + salary 
            + ",My job is to knock code.");
    }
}

07.09_Object-Oriented (static Keyword and Memory Graph) (Understanding)

  • A: Case demonstration
    • Introduce the static keyword through a case.
    • Human: Person. Everyone has a nationality, China.
class Demo1_Static {
    public static void main(String[] args) {
        /*Person p1 = new Person(); //create object
        p1.name = "Teacher Cang;            //Call name properties and assign values
        p1.country = "Japan ";      //Call nationality attributes and assign values


        Person p2 = new Person();
        p2.name = "Miss Ozawa“;       //Call name properties and assign values
        //p2.country = "Japan ";// Call Nationality Attribute and Assignment

        p1.speak();
        p2.speak();*/

        Person.country = "Japan";  //Statically, there is one more way to call, through the class name.
        System.out.println(Person.country);
    }
}

class Person {
    String name;                    //Full name
    static String country;                  //nationality

    public void speak() {           //The Way of Speaking
        System.out.println(name + "..." + country);
    }
}
  • B: Drawing demonstration
    • Memory Graph with static

07.10_Object-Oriented (static Keyword Features) (Master)

  • The Characteristics of A:static Keyword
    • a: Loaded with class loading
    • b: Priority over object existence
    • c: Shared by all objects of the class
      • Example: Students in our class should share the same class number.
      • In fact, this feature is also telling us when to use static?
        • If a member variable is shared by all objects, it should be defined as static.
      • Give an example:
        • Water dispenser (static modification)
        • Water cup (not static)
        • Common use static, characteristic use non-static
    • d: Callable by class name
      • In fact, it can also be called by the object name itself.
      • Class name calls are recommended.
      • Statically modified content is generally referred to as: class-related, class members
  • B: Case demonstration
    • The Characteristics of static Keyword

07.11_Object-Oriented (static Notes) (Master)

  • A: Notes for Static
    • a: There is no this keyword in static methods
      • How to understand it?
        • Static is loaded with class loading, and this exists with object creation.
        • Static is prior to object.
    • b: Static methods can only access static member variables and static member methods
      • Static method:
        • Membership variables: only static variables can be accessed
        • Membership methods: Only static member methods can be accessed
      • Non-static methods:
        • Membership variables: either static or non-static
        • Membership method: But it is a static member method, or it can be a non-static member method.
      • Simple note:
        • Static can only access static.
  • B: Case demonstration
    • Notes for static

07.12_Object-Oriented (Difference between Static Variables and Member Variables) (Master)

  • Static variables are also called class variables, member variables and object variables.
  • A: Different affiliations
    • Static variables belong to classes, so they are also called class variables.
    • Membership variables belong to objects, so they are also called instance variables (object variables).
  • B: Different locations in memory
    • Static variables are stored in the static area of the method area
    • Member variables are stored in heap memory
  • C: Memory appears at different times
    • Static variables are loaded as classes are loaded and disappear as classes disappear.
    • Membership variables exist with the creation of objects and disappear with the disappearance of objects.
  • D: Calls are different
    • Static variables can be called either by class names or by objects.
    • Membership variables can only be invoked by object names

07.13_Object-Oriented (Detailed Explanation of main Method Format) (Understanding)

  • A: format
    • public static void main(String[] args) {}
  • B: Explanation of format
    • public is called by jvm and has sufficient access rights.
    • static is called by jvm and accessed directly by class name without creating objects
    • void is invoked by jvm without requiring a return value to jvm
    • main is a generic name, not a keyword, but recognized by jvm
    • String[] args used to receive keyboard input
  • C: Demonstration case
    • Receiving keyboards, such as data, through args

07.14_Object-Oriented (Static in Tool Classes) (Understanding)

  • A: Making a Tool Class
    • ArrayTool
    • 1. Get the maximum
    • 2. Traversal of arrays
    • 3. Inversion of arrays
/**
This is an array tool class, which encapsulates the methods of finding the maximum value of an array, printing an array, and inverting an array.
@author fengjia
@version v1.0
*/
public class ArrayTool {
    //If all methods in a class are static, we need to do one more step, the private constructor, in order not to let other classes create objects of this class.
    //Use the class name directly. Call it.
    /**
    Private Construction Method
    */
    private ArrayTool(){}

    //1. Get the maximum

    /**
    This is the way to get the maximum value in an array
    @param arr Receive an array of int types
    @return Returns the maximum value in an array
    */
    public static int getMax(int[] arr) {
        int max = arr[0];                       //Record the first element
        for (int i = 1;i < arr.length ;i++ ) {  //Traverse from the second element
            if (max < arr[i]) {                 //Comparing max with other elements in an array
                max = arr[i];                   //Record larger ones
            }
        }

        return max;                             //Return the maximum value
    }
    //2. Traversal of arrays
    /**
    This is the way to traverse arrays
    @param arr Receive an array of int types
    */
    public static void print(int[] arr) {
        for (int i = 0;i < arr.length ;i++ ) {  //foreach
            System.out.print(arr[i] + " ");
        }
    }
    //3. Inversion of arrays
    /**
    This is the method of array inversion.
    @param arr Receive an array of int types
    */
    public static void revArray(int[] arr) {
        for (int i = 0;i < arr.length / 2 ;i++ ) {  //The number of cycles is half the number of elements
            /*
            arr[0]Exchange with arr[arr.length-1-0]
            arr[1]Exchange with arr[arr.length-1-1]
            arr[2]Exchange with arr[arr.length-1-2]
            */
            int temp = arr[i];
            arr[i] = arr[arr.length-1-i];
            arr[arr.length-1-i] = temp;
        }
    }
}

07.15 Object Oriented (Instructions Making Process) (Understanding)

  • A: Document comments for tool classes
  • B: Generate instructions through javadoc commands
    • @ author (extracting author content)
    • @ version (extracting version content)
    • The file directory specified by javadoc-d - author-version ArrayTool. Java
    • @ param parameter name // variable name of formal parameter @return function returns data after running

07.16_Object-Oriented (How to Use the Help Document provided by JDK) (Understanding)

  • A: Find the document and open it.
  • B: Click on the display, find the index, and enter the box.
  • C: You should know who you are looking for? Examples: Scanner
  • D: Look at the structure of this class (no need for a guide)
    • Member variable field
    • Construction method construction method
    • Membership method

07.17_Object-Oriented (Learning the Random Number Function of Math Class) (Understanding)

  • Open the Help Document Learning provided by JDK
  • A: Overview of Math Classes
    • Class contains methods for performing basic mathematical operations
  • B:Math Class Characteristics
    • Because the Math class is under the java.lang package, there is no need to import it.
    • Because its members are all static, it has a private construction method.
  • C: The Method of Obtaining Random Numbers
    • public static double random(): Returns a double value with a positive sign, which is greater than or equal to 0.0 and less than 1.0.
  • D: I want to get a random number between 1 and 100. What should I do?
    • int number = (int)(Math.random()*100)+1;

07.18 Object-Oriented (Guess Digital Game Case) (Understanding)

  • A: Case demonstration
    • Requirement: Guess the number of games (data between 1-100)
/*
* A:Case demonstration
    * Requirement: Guess the number of games (data between 1-100)
*/
import java.util.Scanner;
class Test1_GuessNum {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);                //Create keyboard entry objects
        System.out.println("Please enter an integer,The range is 1.-100 Between");
        int guessNum = (int)(Math.random() * 100) + 1;      //Random number in mind
        while (true) {                                      //Because you need to guess many times, you use infinite loops.
            int result = sc.nextInt();                      //Guess the number
            if (result > guessNum) {                        //If you guess more than I think
                System.out.println("Big");                   //The tip is big.
            } else if (result < guessNum) {                 //If you guess less than I think
                System.out.println("Small");                   //The hint is small.
            } else {                                        //If it's neither big nor small
                System.out.println("Medium");                   //Medium
                break;
            }
        }
    }
}

Posted by LAEinc. Creations on Thu, 21 Mar 2019 23:03:53 -0700