笔记:22/12/09
面向对象:封装:
1、方法:
// 返回值类型, 方法名, 参数列表, 方法体
// 无参无返回值,无参有返回值,有参无返回值,有参有返回值
void a () {
//无参无返回值
}
int b () {
return 1;
//无参有返回值
}
void c (int a ) {
// 有参无返回值
System.out.println(a);
}
int d (int a ) {
return a;
// 有参有返回值
}
-------- 重载:在同一类中,方法名相同,参数列表不同,我们就称之为方法的重载。
class A {
void abc () {
System.out.println(123);
}
void abc (String name) {
System.out.println(name);
}
void abc (int a , String s ) {
System.out.println(a);
System.out.println(s);
}
// -------- 1
void abc (int b , String str) {
System.out.println(b);
System.out.println(str);
}
// 上面的方法与其他abc不构成重载,原因:参数列表相同
// 参数列表: (int a , String s ) (int b , String str) 相等
// 参数列表只看类型,不看变量名
// --------- 2
int abc (int c) {
return 1;
}
// 上面的方法与其他abc构成重载,原因:方法名相同,参数列表不同,与返回值类型无关。
}
2、封装:
三种:
1,把对象,封装成类
2,把属性,封装成变量
3,把行为,封装成方法
class Dog { // 动物:狗 一种类型--->对象
String name ; // 名字 欢欢
int age ; // 年龄 4
int weight ; // 体重 120Kg
void run () { // 动作
// 跑,撒欢
}
void eat () { // 动作
// 吃,啃骨头,吃狗粮
}
}
class People { // 人
// 名字,跑,身高,体重,写作业,
// 属性:名字、身高、体重
// 行为:跑、写作业
String name; int height; int weight;
void run () {}
void xieZuoYe() {}
}
项目:深海大战
1、需求分析:
前期:对象封装完毕,属性和行为未必全都能确定,可能会出现考虑不全面的情况。
/*
是对象:创建类:
主角:战舰
敌人:侦察潜艇,鱼雷潜艇,水雷潜艇
武器:炸弹,水雷
不是对象:所以不需要创建类(坐标:0,0,,,获取图片的时候,直接完成)
背景图:海洋图
图片:开始游戏,暂停图片,结束图片
*/
class BattleShip { // 战舰
int x ; // x坐标
int y ; // y坐标
int height; // 高
int weight; // 宽
void move () {} // 移动方法
}
class ObserveSubmarine { // 侦察潜艇
int x ; // x坐标
int y ; // y坐标
int height; // 高
int weight; // 宽
void move () {} // 移动方法
}
class MineSubmarine { // 水雷潜艇
int x ; // x坐标
int y ; // y坐标
int height; // 高
int weight; // 宽
void move () {} // 移动方法
}
class TorpedoSubmarine { // 鱼雷潜艇
int x ; // x坐标
int y ; // y坐标
int height; // 高
int weight; // 宽
void move () {} // 移动方法
}
class Bomb { // 炸弹
int x ; // x坐标
int y ; // y坐标
int height; // 高
int weight; // 宽
void move () {} // 移动方法
}
class Mine { // 水雷
int x ; // x坐标
int y ; // y坐标
int height; // 高
int weight; // 宽
void move () {} // 移动方法
}
----------------------------------------------------------------+++++++++++-----------------------------------------------------------------
12月09日课程内容:
1、代码优化-------------构造方法(this ,NullPointException)
构造方法:构造器,Construstor,
作用: 赋值
调用:new 类名,自动调用构造方法, 有参传参。
格式:没有返回值标识(void,int,String都没有),方法名与类名完全相同
class Test {
// 构造方法:
Test () {
}
}
赋值方式:
class BattleShip { // 战舰
int x ; // x坐标 20
int y ; // y坐标
int height; // 高
int weight; // 宽
void move () {} // 移动方法
// 20 30 40 50
BattleShip (int x, int y, int height, int weight) {//构造方法
this.x = x; // 20
this.y = y;
this.height = height;
this.weight = weight;
}
// this 代表的是当前对象, this表示的就是类中的成员变量
}
class Test { // 测试类
public static void main(String[] args) {
BattleShip bs = new BattleShip(20,30,40,50);
System.out.println( bs.x ); // 20
System.out.println( bs.y ); // 30
System.out.println( bs.weight ); // 50
System.out.println( bs.height ); // 40
}
}
构造方法: 如果在一个类中,我们不写构造方法,会默认提供一个无参构造
如果在一个类中,我们写了构造方法,都不会默认提供无参构造了。
class A {
A (int a) {
System.out.println(a);
}
}
class B {
public static void main(String[] args) {
A a = new A(23);
}
}
this关键字 ------------ 指代当前对象
class Test {
// 成员变量都有默认值 整形-0 浮点-0.0 boolean-false 引用类型-null
int a ;
void abc () {
int a = 3;
a += 2; // a -> 5
System.out.println(a); // 5
System.out.println(this.a); // 0
}
public static void main(String[] args) {
// System.out.println(this.a); 报错: 原因:static 修饰的方法中没有this
Test t = new Test();
t.abc();
// 5
// 0
}
}
// this 调用普通方法中的使用方式
class ABC {
int a;
void abc () {
System.out.println(123);
}
void abcd () {
System.out.println(1234);
this.abc();
}
public static void main(String[] args) {
ABC abc = new ABC ();
abc.abcd();
// 1234
// 123
}
}
// this 取代构造方法
class QWE {
int a ;
int b ;
QWE () { // 构造方法1
System.out.println(1);
}
QWE (int a) { // 构造方法2
this(); // 调用本类的构造方法, 如果没有参数,调用的就是无参构造
System.out.println(2);
}
public static void main(String[] args) {
// QWE q = new QWE(34); // 2
// 结果中,先输出1 在输出2
QWE q = new QWE(34); // 1 2
}
}
NullPointerException : 空指针异常(一日三空,未来的生活)
class A {
public static void main(String[] args) {
// 此代码不是 : NullPointerException
// String str = null;
// System.out.println(str);
// 此代码是 : NullPointerException
// String stra = null;
// System.out.println(stra.length());
// .length() 代表字符串的长度
// 此代码不是 : NullPointerException
int[] arr = null;
System.out.println(arr);
// 此代码是 : NullPointerException
int[] arrr = null;
System.out.println(arrr.length);
// 如果一个引用类型的值为 null
// 被使用不会发生空指针异常,如,输出arr
// 在调用其他变量或方法时,会发生空指针异常,如,arr.length
// arr.length 调用 arr 中的 length 变量
}
}
Idea快捷键:
// sout 输出
// soutv 自动拼接输出内容
// 值.var 自动生成变量
// Ctrl + c 赋值
// Ctrl + v 粘贴
// Ctrl + z 撤销
// Ctrl + / 选中注释(选中代码后使用,全部注释)
// Ctrl + Alt + L 规范代码
// Alt + insert 选择 Construstor 自动生成构造方法
// Alt + insert 选择 getter and setter 自动生成 get 和 set 方法
// Alt + F4 别轻易尝试(关闭当前页面,如果没有页面,关机)
// 右键-----refactor----rename 重命名
// 右键-----delete 删除
// Ctrl + D 复制文本到下一行(Ctrl + 向下的箭头)