1. Packaging
java has three characteristics: encapsulation, inheritance and polymorphism
Encapsulation is also a programming idea of java.
Class's encapsulation package's encapsulation method's encapsulation property's encapsulation
Below, we mainly talk about the encapsulation of member variables (attributes)
When writing a class, the previous member variables are decorated with modifiers? No, anyone can use this variable
Just pass through the object Properties can operate our properties, which has hidden dangers.
You can add a restriction.
1. Use the private keyword to modify member variables
2. Create set and get methods corresponding to member variables
3. Object The set method assigns an object get method value
public class Fruit { //Member variable private String kind;//type private double price;//Price //The function of the setKind method is to assign a value to the variable kind public void setKind (String kind) { this.kind = kind; } //The function of getkind method is to value the variable kind public String getKind() { return kind; } public void setPrice(double price) { this.price = price; } public double getPrice () { return price; } }
[precautions]:
1. Modify the member attribute private
2. Write the get and set methods of the corresponding attributes (idea has shortcut keys)
3. From today on, we will standardize the class, add private to all member variables in the future, and write get and set methods below.
4. Purpose. We can standardize our operations on objects in future projects
5. It is mainly the logic that can hide part of the code
2.this keyword
Literally:
this
1. Use this to represent the current class object.
You can use this to call the member properties (common) and member methods of the current class
2.this can also call the constructor [rarely used!!!]
1., you can only call another construction method in the construction method.
2. This (with actual parameters) can only be written in the first line in the construction method.
3. You cannot call your own constructor
class Dog { private String name; private int age; private char sex;//Gender public Dog() { System.out.println("I am a parameterless construction method"); } public Dog(String name) { //You can't call yourself when using this //this("Xiao Huang"); System.out.println("I am with name Construction method of parameters" + name); } public Dog (String name, int age, char sex) { //this is also calling the member property // this.name = name; this("son of a bitch"); this.age = age; this.sex = sex; //You want to call a parameterless construct in this constructor //this(); } public void setName (String name) { //this is the member property called this.name = name; } public void eat() { //The sleep method needs to be called in the eat method //Use this keyword to call member methods this.sleep(); System.out.println("The dog is eating!!!"); } public void sleep() { System.out.println("Don't scare the dog when he is sleeping"); } } public class Demo1 { public static void main(String[] args) { Dog dog = new Dog("Wangcai", 3, 'common'); //dog.eat(); } }
3. Multi type cooperation
1. Human beings
Attribute: name age sex cat
2. Cats
Attribute: name age
A member variable of the human class is the cat object
public class Cat { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
public class Person { private String name; private int age; private boolean sex; private Cat cat;//An object is treated as a property of another class public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public boolean isSex() { return sex; } public void setSex(boolean sex) { this.sex = sex; } public Cat getCat() { return cat; } public void setCat(Cat cat) { this.cat = cat; } }
public class Demo2 { public static void main(String[] args) { Cat cat = new Cat(); cat.setName("chinese rhubarb"); cat.setAge(3); Person person = new Person(); person.setName("Zhang San"); person.setAge(29); person.setSex(true); person.setCat(cat); System.out.println(person.getName());//Wang Heng System.out.println(person.getAge());//29 System.out.println(person.isSex());//true System.out.println(person.getCat().getName());//chinese rhubarb System.out.println(person.getCat().getAge());//3 } }
4. Anonymous objects
new Person();
Syntax format:
create object
Class name object name = new class name ();
The left side of the assignment number is just a variable defined in the stack area of memory. Reference data
The heap area in the memory on the right side of the assignment number creates a class object, which can be assigned to real data
Anonymous object creation format:
The new class name () and the things to the left of the equal sign are gone. But it's disposable
class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void eat() { System.out.println("Braised chicken for lunch today,Have some porridge in the evening!!!"); } }
public class Demo1 { public static void main(String[] args) { new Person().eat(); new Person().setName("son of a bitch"); String name = new Person().getName(); System.out.println(name);//null Person person = new Person(); person.setName("Cuihua"); String name1 = person.getName(); System.out.println(name1); System.out.println(new Person()); System.out.println(new Person()); //Anonymous objects are generally used as parameters of a method test(new Person()); // Person person1 = new Person(); // test(person1); } public static void test (Person person) { person.eat(); } }