Object-oriented overview
java is object-oriented high-level programming language. Simple things can be considered to be a high degree of code to simulate the real world: the object oriented implementation.
Object-oriented of the most important two concepts: classes, objects.
Class of understanding
- Class is a description of common attributes and behavior of the same thing. Humans, like students, teachers, class, officials, businessmen.
Simple to understand: The class is a description of the reality of things
The composition of the class
- Attributes (member variables): refers to the characteristics of things, such as: human (name, age, height, weight)
- Behavior (member method): refers to operations that things can be performed, such as: human (singing, dancing)
Objects
- Everything that exists objectively are all objects, so we often say things are objects.
- Objects are instances of real existence. Object == instance.
Class definition
Class usually seen two components:
the composition is determined by the class properties and behavior of two parts
- Properties: In a class by member variables to reflect
- Behavior: in the classes to reflect the methods by members
Class Definition Format:
修饰符 class 类名{
// 1、成员变量:Field(描述类或者对象的属性信息的,例如:身高,体重,名称)
// 2、成员方法: Method(描述类或者对象的行为信息的,例如:唱歌,跳舞,买票)
// 3、构造器: Constructor(初始化类的一个对象出来)
....
}
Class defined in claim
1, class name first letter capitalized recommendations to meet hump mode , can not use keywords, identifiers must be lawful and to be meaningful.
2, a Java file can define multiple classes, but only one class modified with public and public-modified class name, the file must be the name of the Java code, otherwise an error!
3, a Java code file a class can not be modified with the public, then the code file name can be freely
4, but it is recommended that a java file only defines one class.
E.g:
public class People {
// 1、属性
String name; // 名称
int age ; // 年龄
double height ; //身高
double weight ; // 体重
// 2、行为
public void sing(){
System.out.println("唱的真好!!");
}
public void jump(){
System.out.println("跳的真美!!");
}
}
Creating objects
Creating an object format:
name of the class object name = new class name ();
call the member variable format:
Object variable members
calling member methods format:
name of the object member method ();.
public class PeopleDemo {
public static void main(String[] args) {
// 1、创建对象的格式:
// 类名 对象名称 = new 类名();
People wuHao = new People();
// 2、调用成员变量的格式:对象名.成员变量
System.out.println(wuHao.name); // null
wuHao.name = "吴昊"; // 赋值
wuHao.age = 18 ; // 赋值
wuHao.weight = 60; // 赋值
wuHao.height = 180 ;// 赋值
System.out.println(wuHao.name); // 吴昊
System.out.println(wuHao.age); // 18
System.out.println(wuHao.weight); // 60.0
System.out.println(wuHao.height); // 180.0
// 3、调用成员方法的格式:对象名称.成员方法
wuHao.sing();
wuHao.jump();
}
}
class People {
// 1、属性
String name; // 名称
int age ; // 年龄
double height ; //身高
double weight ; // 体重
// 2、行为
public void sing(){
System.out.println("唱的真好!!");
}
public void jump(){
System.out.println("跳的真美!!");
}
}
Note: Objects can directly assign themselves to another object, this address assignment, which will be another long modify a modification. With reference to the type of array bit like.
Student minGe = xuMin; // 把xuMin这个对象变量存储的地址赋值给了minGe变量
example:
public class StudentDemo {
public static void main(String[] args) {
Student xuMin = new Student();
System.out.println(xuMin); // 6d03e736
xuMin.name = "徐敏";
xuMin.age = 18 ;
System.out.println(xuMin.name); // 徐敏
System.out.println(xuMin.age); // 18
xuMin.study();
xuMin.doHomeWork();
System.out.println("-----------------------------------");
Student minGe = xuMin; // 把xuMin这个对象变量存储的地址赋值给了minGe变量
System.out.println(minGe); // 6d03e736
minGe.name = "敏哥";
System.out.println(xuMin.name); // 敏哥
System.out.println(minGe.age); // 18
minGe.study();
minGe.doHomeWork();
}
}
class Student{
// a.属性(成员变量)
String name ;
int age ;
// b.行为(方法):学习
public void study(){
System.out.println("好好学习,天天向上");
}
// c.行为(方法):抄写代码
public void doHomeWork(){
System.out.println("写代码、写代码、写代码。");
}
}