oop.day03

回顾:
1.猜字符小游戏
2.什么是类?什么是对象?
1)现实世界是由很多很多对象组成的
基于对象抽出了类
2)对象是真正存在的单个的个体
类代表一类个体,类型,类别
3)类中包含:
3.1)所有对象所共有的特征/属性---------变量
3.2)所有对象所共有的行为----------------方法
4)一个类可以创建多个对象
同一个类的多个对象,结构相同,数据不同
5)类是对象的模板,对象是类的具体的实例

笔记:
1.如何创建类?如何创建对象?
2.引用类型之间画等号: 家门钥匙
1)指向同一个对象(数据有一份)
2)对其中一个引用的修改,会影响另外一个引用
基本类型之间画等号: 身份征复印件
1)赋值(数据有两份)
2)对其中一个值的修改,不会影响另外一个值
3.null:
1)空,即:不再指向任何对象
2)若引用的值为null,则不能再进行任何操作了
若操作则NullPointerException(空指针异常)
4.方法签名=方法名+参数列表
5.方法的重载(overload):
1)一个类中,方法名称相同,参数列表不同,称方法的重载
2)编译器在编译时,自动根据方法的签名绑定调用不同的方法
6.构造方法:
1)给成员变量赋初值
2)与类同名,没有返回值类型
3)在创建(new)对象时被自动调用
4)若自己不写构造,则系统默认提供一个无参构造
若自己写构造,则不再默认提供
5)构造方法可以重载
7.this关键字:
1)指代当前对象,哪个对象调指的就是哪个对象
方法中访问成员变量,默认有个this.
2)用法:
2.1)this.成员变量名---------访问成员变量
2.2)this.方法名()-------------调用方法(几乎不用)
2.3)this()-----------------------调用构造方法


任务:
1.做Cell类的3个构造方法,并测试
2.看Demo.java与OverloadDemo.java-----自己做小例子
3.Cell和CellTest再做一次
4.课后作业---第1天的全部,第2天的前两个做了
5.每日一练


Cell c = new Cell(2,5);
c.drop();
c.moveLeft(3);
String str = c.getCellInfo();

Cell c1 = new Cell(); //0,0
Cell c2 = new Cell(3); //3,3
Cell c3 = new Cell(2,5); //2,5


class Cell{
int row;
int col;
Cell(){
this(0,0); //调用构造方法
}
Cell(int n){
this(n,n); //调用构造方法
}
Cell(int row,int col){
this.row=row;
this.col=col;
}
void drop(){
row++;
}
void moveLeft(int n){
col-=n;
}
String getCellInfo(){
return row+","+col;
}
}


Cell c = new Cell();
c.row = 2;
c.col = 5;
c.drop();

Cell cc = new Cell();
cc.row = 6;
cc.col = 3;
cc.drop();

Cell ccc = new Cell();
ccc.drop();


Cell c = new Cell(2,5);
c.drop();


class Cell{
int row; //成员变量
int col;
Cell(int row,int col){ //局部变量
this.row = row; //c.row=2
this.col = col; //c.col=5
}
void drop(){
row++; //c.row++
}
}


Student zs = new Student();
Cell c = new Cell();

//1.创建一个Student对象
//2.调用构造方法
Student zs = new Student("zhangsan",25,"河北廊坊");
Student ls = new Student("lisi",26,"佳木斯");
Student zs = new Student(); //编译错误

class Student{
String name;
int age;
String address;
Student(){
}
//给成员变量赋初值---应用率非常高
Student(String name1,int age1,String address1){
name = name1;
age = age1;
address = address1;
}
}

Student zs = new Student();
zs.setInfo("zhangsan",25,"河北廊坊");

Student ls = new Student();
ls.setInfo("lisi",26,"黑龙江佳木斯");


Student zs = new Student();
zs.name = "zhangsan";
zs.age = 25;
zs.address = "河北廊坊";

Student ls = new Student();
ls.name = "lisi";
ls.age = 26;
ls.address = "黑龙江佳木斯";

System.out.println("helloworld");
System.out.println(250);
System.out.println(8.88);
System.out.println(true);
System.out.println('a');
System.out.println();

1)找对象: 一堆小格子
2)抽类: 格子类Cell
3)设计类中的成员变量和方法
4)创建对象、访问成员变量以及调用方法

//new出来的是对象,对象是一个数据
new Student();
new Student();

Student zs = new Student();
Student ls = new Student();

zs.name = "zhangsan";
zs.age = 24;
zs.address = "";


类(类型) 引用类型变量(引用) 对象
Student zs = new Student();
对zs的操作就是对对象的操作


面向对象:难(抽象)

合理不合理----------------

int num; //基本类型变量(变量)


1)下落一格
2)左移n格
3)获取格子行号和列号

Cell
CellTest


class Cell{
int row; //行号
int col; //列号
void drop(){ //下落一格
row++;
}
void moveLeft(int n){ //左移n格
col-=n;
}
String getCellInfo(){ //获取格子行号和列号
return row+","+col;
}

}


变量分两种:
1.局部变量: 方法中
从声明开始,到包含它最近的大括号结束
2.成员变量: 类中,方法外
整个类


class Aoo{
int num;--------------成员变量
void show(){
int num2;-----------局部变量
}
}

原文地址:https://www.cnblogs.com/xiaziteng/p/4722665.html