JAVA-basic concepts

Objects, classes, methods, instance variables

  1. Class: A class is a template that describes the properties and methods of a class of objects.
  2. Object: An object is an instance of a class with attributes and methods.
  3. Method: Method is behavior, a class can have many methods.
  4. Instance variables: Each object has a unique instance variable, and the state of the object is determined by the value of these instance variables.

[Example] Cats, dogs, and elephants can be abstracted into a category called animals, in which a dog, a cat, and an elephant are all objects
. The attributes of a dog are: color, name, breed, etc.;
the method of a dog: shake Tail, call, eat, etc.

//类的定义
public class Dog {
    
    
	//属性
    String breed;
    int size;
    String colour;
    int age;
    //方法
    void eat() {
    
    
    }
    void run() {
    
    
    }
    void sleep(){
    
    
    }
    void name(){
    
    
    }
}
//构造方法
/**每个类都有构造方法。
如果没有显式地为类定义构造方法,Java 编译器将会为该类提供一个默认构造方法。
在创建一个对象的时候,至少要调用一个构造方法。构造方法的名称必须与类同名,一个类可以有多个构造方法
**/
public class Dog{
    
    
	//属性
    String breed;
    int size;
    String colour;
    int age;
    //方法
    void eat() {
    
    
    }
    void run() {
    
    
    }
    void sleep(){
    
    
    }
    void name(){
    
    
    }
    public Dog(){
    
    
    //默认构造
    }
    public Dog(String name){
    
    
        // 仅有一个参数构造
    }
}
//创建对象
/*
创建对象需要以下三步:
声明:声明一个对象,包括对象名称和对象类型。
实例化:使用关键字 new 来创建一个对象。
初始化:使用 new 创建对象时,会调用构造方法初始化对象。
*/
public static void main(String[] args){
    
    
      // 下面的语句将创建一个Dog对象
      Dog myDog = new Dog( "tommy" );
      Dog hisDog= new Dog();
   }
//访问属性和方法
myDog.size;
myDog.eat();

JAVA basic grammar notes

  • Case Sensitive

Hello and hello are different

  • Class name: The first letter of the class name should be capitalized. If the class name consists of several words, then the first letter of each word should be capitalized

F 例】 MyFirstJavaClass

  • Method name: All method names should start with a lowercase letter. If the method name contains several words, capitalize the first letter of each subsequent word.

  • Source file name: The source file name must be the same as the class name. If the file name and the class name are not the same, a compilation error will result.

  • Main method entrance: all Java programs are executed by the public static void main(String[] args) method.
    Insert picture description here

//java基本结构 
public class HelloWorld {
    
    
    public static void main(String[] args) {
    
    
    }
}
  • Identifier: All components need names. Such as class name, variable name and method name
    • Should start with a letter (AZ or az), dollar sign ($), or underscore (_)
    • Keywords cannot be used as identifiers
    • Identifiers are case sensitive

[Example]
Legal identifiers: age, $salary, _value, __1_value
Illegal identifiers: 123abc, -salary

  • Modifiers: modify methods and attributes in the class

Access control modifiers: default, public, protected, private
Non-access control modifiers: final, abstract, static, synchronized

  • Variables: local variables, class variables (static variables), member variables (non-static variables)

Local variables: Variables defined in methods, construction methods, or statement blocks are called local variables. Variable declaration and initialization are in the method. After the method ends, the variable will be automatically destroyed.
Member variables: Member variables are variables defined in the class, outside the method body. This variable is instantiated when the object is created. Member variables can be accessed by class methods, construction methods, and specific class statement blocks.
Class variables: Class variables are also declared in the class, outside the method body, but must be declared as static types.

Guess you like

Origin blog.csdn.net/Christina_cyw/article/details/112861123