One member of the class: Properties

1. How to declare property?

// no explicit initialization, has a default value
[data type] attribute names modifier;
// explicitly initialized, manual assignment
[Modifier] Data type attribute name = value;
Description: type attribute may be any type of Java, including basic data types, reference data types (classes, interfaces, arrays, etc.)
class boy{
    String name;
    int age;
    Girl girlFriend;
}
class Girl{
    String name;
    int age;
    Boy boyFriend;
}

  

Summary: Java data types

(1) The basic data types (8 types)

byte,short,int,long,float,double,char,boolean

(2) reference data types

① categories:

例如:String、Student、Circle、System、Scanner、Math...

② Interface:

③ array:

For example: int [], String [], char [], int [ ] [ ]

int [] arr = new int [ 5];
where the int [] array as type, a reference data types, the object is to the right of the assignment of an array
Data element type: int
data type array: int []

2. How property assignment?

(1) when the explicit assignment statement properties

[Modifier] Data type attribute name = value; 
class Student{
   String name;
   char gender = '男';
}

(2) After the assignment to create objects

// create an object 
class name object name = new class name (); // assign property of the object object name attribute name = value;


class Student{
   String name;
   char gender = '男';
}
class TestStudet{
   public static void main(String[] args){
       Student stu = new Student();
       stu.name = "张三";
       stu.gender = '女';
  }
}

3, how to access the property value?

(1) In access process of this class

direct interview

 

Sample code:

class Circle{
  double radius;
   
  double area(){
      return 3.14 * radius * radius;
  }
}

(2) access methods in other classes

Object Name Property

Code Example:

class Circle { Double RADIUS; } class TestCircle { public static void main ( String [] args) { // example: printing attribute value Cirlce Yuan = new new Circle (); . the System . OUT the println ( . Yuan RADIUS); / / example: Comparative properties of two objects Cirlce C1 = new new Circle (); C1. RADIUS = 1.2; Cirlce C2 = new new Circle (); C2. RADIUS = 1.2;
   


   
       
       



       
       
       

       
       
       
       System.out.println(c1.radius == c2.radius);        
  }
}

4, the characteristics of the property

(1) is the default attribute value

(2) the property value of each object is independent

 

 

Guess you like

Origin www.cnblogs.com/panyizuoshan/p/11448483.html