Method and deepening

  • Method definition

    • The modifier
    • The return type
    • Break: Breaks out of the switch, ending the distinction between loop and return
    • Method name: Note that the specification is known by name
    • Parameter list (parameter type, parameter name)…
    • Exception is thrown
    Public class Demo01 {// main method public static void main(String[] args) {} /* {// method body return value; } */ / return returns a result! public String sayHello(){ return "hello,world"; } public void print(){ return; } public int max(int a,int b){ return a>b ? a : b; // Ternary operator}}Copy the code
  • Method call

    • A static method
    • Nonstatic method
    • Parameters and arguments
    • Value passing and reference passing
    • This keyword
    Package com.opp.demo01 public class Demo2 {public static void main(String[] args){// Static method student.say (); // Non-static method // instantiate this class new // object type object name = object value; Student student = new Student(); student.say(); } public static void a(){b(); / / an error after} / / class instantiation to existing public void b () {}} / / = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = package com. Oop. Demo01; Public class Student{// static method public static void say(){system.out.println (" Student said "); } // Non-static method public void say2(){system.out.println (" student speak "); }}Copy the code
    Public static boid main(String[] args){public static boid main(String[] args){int add = demo03.add (1,2); System.out.println(add); } public static int add(int a,int b){ return a+b; }Copy the code
    Public class Demo04{public static void main(String[] args){int a = 1; System.out.println(a); // 1 Demo04.change(a); System.out.println(a); Public static void change(int a){a = 10; }}Copy the code
    Public class Demo05{public static void main(String[] args){Person Person = new Person(); System.out.println(person.name); // null Demo05.change(person); System.out.println(person.name); Person Person = new Person(); Person Person = new Person(); This is a concrete person who can change attributes! Person. Name = "person "; } // Define a Person class with one attribute: name class Person{String name; }Copy the code

Relationships between classes and objects

  • A class is an abstract data type that describes/defines a class of things as a whole, but does not represent a specific thing.

    • Animals, plants, cell phones, computers…
    • Person class, Pet class, Car class, etc. These classes are used to describe/define the characteristics and behavior of a class-specific thing
  • An object is a concrete instance of an abstract concept

    • Zhang SAN is a specific example of people, zhang SAN’s family wealth is a specific example of the dog.
    • It is a concrete instance, not an abstract concept, that shows the characteristics and functions.

Create and initialize objects

  • Use the new keyword to create an object

  • When the new keyword is used, in addition to allocating memory, the created object is initialized by default and the constructor in the class is called.

  • Class constructors are also called constructors and must be called when creating objects. And the constructor has the following two characteristics:

    • 1. It must have the same name as the class
    • There must be no return value type, and void cannot be written
  • Constructors must be mastered

  • Code example (class and object creation) :

Public static void main(String[] args) {public static void main(String[] args) { // The student object is a concrete instance of the student class! Student xiaoming = new Student(); Student xh = new Student(); Name = "xiaoming "; xiaoming.age = 3; System.out.println(xiaoming.name); System.out.println(xiaoming.age); Xh. Name = "xh "; xh.age = 3; System.out.println(xh.name); System.out.println(xh.age); } // class Student public class Student{// attribute: String name; // null int age; Public void study(){system.out.println (this.name+" learning "); }}Copy the code
  • Example code (constructor details) :
Public static void main(String[] args) {//new instantiate an object Person = new Person(); System.out.println(person.name); // public class Person {// public class Person {// public class Person {// public class Person {// public class Person; Public Person() {this.name = "LiBo"} public Person() {this.name = "LiBo"} Public Person(String name){this.name = name; public Person(String name){this.name = name; } // Alt + insert quick create constructor}Copy the code
  • conclusion
  • Constructor: 1, same as class name 2, no return value
  • Function:
  • 1. New essentially calls the constructor
  • 2. Initialize the object value
  • Note:
  • 1, after defining a parameterized construct, if you want to use a parameterless construct, display a parameterless construct

Briefly summarize classes and objects

  1. Classes and objects
    • A class is a template: an abstraction, and an object is a concrete column
  2. methods
    • Define, invoke!
  3. Corresponding reference
    • Reference Types, Primitive Types (8)
    • Objects operate by reference: stack —-> heap
  4. Property: Field member variable
    • Default initialization:
      • Numbers: 0 0.0
      • Char: u0000
      • Boolean: false
      • Reference: null
    • Attribute type Attribute name = attribute value;
  5. Object creation and use
    • The object must be created using the new keyword. The constructor Person muzi = new Person();
    • Object property muzi.name
    • Object method muzi.sleep();
  6. class
    • Static attribute attributes
    • Dynamic behavior methods

The last

Finally provide free Java architecture learning materials, learning technology content includes: Spring, Dubbo, MyBatis, RPC, source code analysis, high concurrency, high performance, distributed, performance optimization, micro services advanced architecture development and so on. Pay attention to my public number future light can be obtained!