First, the object-oriented three main study :
1, the object class and class: properties, methods, constructors, code blocks, inner classes.
2, object-oriented three characteristics: encapsulation, inheritance, polymorphism (sometimes plus abstract)
3, a number of other keywords: this, super, static, abstract , final, interface, package, import the like.
Second, the difference between the object-oriented and process-oriented: (people-elephant put into the refrigerator, for example)
1, process-oriented: emphasis is functional behavior, consideration is how to do it; as a function of the smallest unit.
example:
- ① open the refrigerator door;
- ② the elephant put into the refrigerator;
- ③ refrigerator door closed;
2, object-oriented: emphasized includes function object, who is considered to do; to class / object as a minimum unit.
example:
* 人{
* 打开(冰箱){
* 冰箱.开开();
* }
*
* 抬起(大象){
* 大象.进入(冰箱);
* }
*
* 关闭(冰箱){
* 冰箱.闭合();
* }
*
* }
*
*
* 冰箱{
* 开开(){}
* 闭合(){}
* }
*
* 大象{
* 进入(冰箱){
* }
* }
Third, the object-oriented two elements:
1, Class: class is the description of a class of things, is a conceptual abstract definition;
2, the object: the object is actually present in each individual certain things and, therefore, called instance; (instance)
so the object-oriented thinking focused on the design of the class, but the class is the design of the actual members of the design class.
Four members of the class:
Attribute = = variable domain member, field = field
method = member functions method = method =
Object class = class is created to instantiate instances of classes =
Five classes and objects using: (Object Oriented landing)
1, create a class
2, class creates objects
3, by calling the object structure of the object and the properties of the object of the method.
If you create a class of multiple objects, each object has a separate set of attribute class.
That is, if the attribute value of the class to modify the properties of an object does not affect another object.
Memory resolve objects:
anonymous object: the object we created, did not explicitly assigned to a variable name. That is, anonymous objects
Features: anonymous object can only be called once.
For example:
new Phone().sendEmail();
new Phone().playGame();
new Phone().price = 1999;
new Phone().showPrice();//0.0
Scenario:
PhoneMall mall = new PhoneMall();
//匿名对象的使用
mall.show(new Phone());
class PhoneMall{
public void show(Phone phone){
phone.sendEmail();
phone.playGame();
}
}
Design class, two important structures: Properties and Methods
Contrast: Properties vs local variables
1. The same point:
1.1 format defined variables: variable name = data type variable value
1.2 the first statement, the use of
1.3 variables corresponding scope
2. Different points:
2.1 in different positions in the class declared
properties: a pair of directly defined within {} class
local variables: variables in the process, process parameter, the code block, the constructor parameter, the internal statement constructor
2.2 different modifiers on rights of
property: When can declare property, indicating its rights, usage rights modifier.
Common permissions modifiers: private, public, default, protected -> encapsulation
Currently, when we declared properties, use the default on it.
Local variables: You can not use Permissions modifier.
2.3 默认初始化值的情况:
属性:类的属性,根据其类型,都默认初始化值。
整型(byte、short、int、long:0)
浮点型(float、double:0.0)
字符型(char:0 (或’\u0000’))
布尔型(boolean:false)
引用数据类型(类、数组、接口:null)
局部变量:没默认初始化值。
意味着,我们在调用局部变量之前,一定要显式赋值。
特别地:形参在调用时,我们赋值即可。
2.4 在内存中加载的位置:
属性:加载到堆空间中 (非static)
局部变量:加载到栈空间
回顾变量的分类
方式一:按照数据类型:
方式二:按照在类中声明的位置:
public class UserTest {
public static void main(String[] args) {
User u1 = new User();
System.out.println(u1.name);
System.out.println(u1.age);
System.out.println(u1.isMale);
u1.talk("韩语");
u1.eat();
}
}
class User{
//属性(或成员变量)
String name;
public int age;
boolean isMale;
public void talk(String language){//language:形参,也是局部变量
System.out.println("我们使用" + language + "进行交流");
}
public void eat(){
String food = "烙饼";//局部变量
System.out.println("北方人喜欢吃:" + food);
}
}
类中方法的声明和使用
方法:描述类应该具有的功能。
比如:
Math类:sqrt()\random() …
Scanner类:nextXxx() …
Arrays类:sort() \ binarySearch() \ toString() \ equals() \ …
1.举例:
public void eat(){}
public void sleep(int hour){}
public String getName(){}
public String getNation(String nation){}
- 方法的声明:
权限修饰符 返回值类型 方法名(形参列表){
方法体
}
注意:static、final、abstract 来修饰的方法,后面再讲。
- 说明:
3.1 关于权限修饰符:默认方法的权限修饰符先都使用public
Java规定的4种权限修饰符:private、public、缺省、protected -->封装性再细说
3.2 返回值类型: 有返回值 vs 没有返回值
3.2.1 如果方法有返回值,则必须在方法声明时,指定返回值的类型。同时,方法中,需要使用return关键字来返回指定类型的变量或常量:“return 数据”。 如果方法没有返回值,则方法声明时,使用void来表示。通常,没有返回值的方法中,就不需要使用return.但是,如果使用的话,只能“return;”表示结束此方法的意思。
3.2.2 我们定义方法该不该有返回值?
① 题目要求
② 凭经验:具体问题具体分析
3.3 方法名:属于标识符,遵循标识符的规则和规范,“见名知意”
3.4 形参列表: 方法可以声明0个,1个,或多个形参。
3.4.1 格式:数据类型1 形参1,数据类型2 形参2,…
3.4.2 我们定义方法时,该不该定义形参?
① 题目要求
② 凭经验:具体问题具体分析
3.5 方法体:方法功能的体现。
4、return关键字的使用:
1.使用范围:使用在方法体中
2.作用:① 结束方法
② 针对于有返回值类型的方法,使用"return 数据"方法返回所要的数据。
3.注意点:return关键字后面不可以声明执行语句。
5、方法的使用中,可以调用当前类的属性或方法
特殊的:方法A中又调用了方法A:递归方法。
注意: 方法中,不可以定义方法。
public class CustomerTest {
public static void main(String[] args) {
Customer cust1 = new Customer();
cust1.eat();
//测试形参是否需要设置的问题
// int[] arr = new int[]{3,4,5,2,5};
// cust1.sort();
cust1.sleep(8);
}
}
//客户类
class Customer{
//属性
String name;
int age;
boolean isMale;
//方法
public void eat(){
System.out.println("客户吃饭");
return;
//return后不可以声明表达式
// System.out.println("hello");
}
public void sleep(int hour){
System.out.println("休息了" + hour + "个小时");
eat();
// sleep(10);
}
public String getName(){
if(age > 18){
return name;
}else{
return "Tom";
}
}
public String getNation(String nation){
String info = "我的国籍是:" + nation;
return info;
}
//体会形参是否需要设置的问题
// public void sort(int[] arr){
//
// }
// public void sort(){
// int[] arr = new int[]{3,4,5,2,5,63,2,5};
// //。。。。
// }
public void info(){
//错误的
// public void swim(){
//
// }
}
}