Java Note

类与对象

  • Java分为两种数据类型:
    • 基本数据类型;
    • 引用数据类型(String和我们创建的所有类);
  • 类:
    • 属性:这类事物拥有的共有属性;
    • 动作:这一类事物共同执行的功能;
  • 对象:
    • 使用类创建的具体某一个东西;
  • 写代码:
    • 类要用class来定义;
    • 属性:成员变量来描述,直接写在类中的变量;
    • 动作:成员方法,不屑static就是成员方法;
    • 创建对象:
      类 引用 = new 类();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Car{
String color;// 颜色
int speed;// 速度
int seat = 5;// 座位

// 成员方法
public void run(){
System.out.println("车能跑");
}

public static void main(String[] args){
int a = 10;// 写在方法里的变量,局部变量
// 创建对象;创建了一辆车,后面想用这辆车,就需要使用c来访问
Car c = new Car();// 在面向对象的世界里,变量是没有市场的,这种变量被称为引用;即c被称为引用
// 对象或者引用.方法()
c.run();// .表示调用
c.color = "绿色";
c.speed = 120;
// 类中没有定义的内容不可以使用
System.out.println(c.color);
}
}

this关键字

this:

  • 当前类的对象;
  • 可以在方法内部获取到对象中的属性信息;
  • 还可以区分局部变量和成员变量;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class Car{
String color;
int speed;
int seat = 5;

public void run(){
// 默认会有一个this;当前正在执行这个方法的对象
// 获取到车的颜色和速度
System.out.println(this);
System.out.println(this.color);
System.out.println(this.speed);
System.out.println("车能跑");
}

public void fly(String color){
System.out.println(color + "颜色的车会飞");
System.out.println(this.color + "颜色的车会飞");
//这里的color变量不同,前者是局部变量,后者是成员变量;
}

public static void main(String[] args){
// Car c = new Car();// 车中的属性就是类中定义好的成员变量
// c.color = "绿色";
// c.speed = 120;

// c.run();// 在调用方法的时候,Java会自动的把对象传递给方法,在方法中由this来接收对象
// System.out.println(this);
// System.out.println(c.color);
// Car c2 = new Car();
// c2.color = "红色";
// c2.speed = 180;

// c2.run();

// this可以帮我们区分成员变量和局部变量
Car c = new Car();
c.color = "绿色";

c.fly("黑色");
}
}

构造方法

在创建对象的时候,自动调用的方法;

语法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class Car{

String color;
int speed;
int seat = 5;

// Java会自动的赠送每一个类一个无参数的构造方法
// 如果你自己定义了构造方法,Java就不再赠送了
// 在创建对象的时候,自动调用的方法
public Car(){
System.out.println("无参数的构造方法被调用了");
}// 构造方法没有返回值,也不能写void;如果没有这一部分,由于后面定义了Car类,Java不会赠送无参构造方法
public Car(String color, int speed){
System.out.println("有参数的构造方法被调用了");
}
public void run(){
System.out.println(this.color + "的车在跑");
}
public static void main(String[] args){
Car c1 = new Car();// 默认调用的构造方法
c1.color = "绿色";
c1.speed = 100;

c1.run();

Car c2 = new Car();
c2.color = "绿色";
c2.speed = 120;

c2.run();

}
}

注意:

  • 没有返回值这一项;
  • 在我们new的时候,自动调用构造方法;

作用:在创建对象的时候,给对象设置属性信息;

Java会默认自动的送给每一个类一个无参数的构造方法,但是,如果你写了构造方法,系统就不在 赠送了;

构造方法重载

构造方法也是方法,也可以进行重载;

作用:可以有更多的方法去创建对象;

使用this可以访问其他的构造方法;

this();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class DaXia {
String name;
String waihao;
int age;
String bangpai;

// 我们需要两个构造方法
public DaXia(String name, int age, String bangpai) {
this.name = name;
this.age = age;
this.bangpai = bangpai;
}

// 构造方法也是方法,也可以进行重载
// 可以让我们又更多的方法去创建对象
public DaXia(String name, String waihao, int age, String bangpai) {
// this.name = name;
// this.waihao = waihao;
// this.age = age;
//这是常规写法,这里可以通过this调用当前类中的其他构造方法
this(name, age, bangpai);
this.bangpai = bangpai;
}

public static void main(String[] args) {

// 岳不群
DaXia yuebuqun = new DaXia("岳不群", 18, "华山派");

// 武松
DaXia wusong = new DaXia("武松", "行者", 19, "梁山");
}
}

static

static.静态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Person {
String name;
static String country;
// String country;
String address;

public Person(String name, String address) {
this.name = name;
this.address = address;
//this.country = country;由于是静态的,这里就不用传入了;
}
public static void main(String[] args) {
Person p1 = new Person("赵铁柱", "八大胡同");
Person p2 = new Person("李小花", "朝阳门");

// 大清亡了,这里如果有多个人,写起来会很麻烦
// 所以在Person类里面,将country设置为静态可以共享
Person.country = "民国";// 建议写成这种格式,由类名访问
// p1.country = "民国";// 这里只要更改一个,所有的country都会变化,因为是共享的
// p2.country = "民国";

System.out.println(p1.country);
System.out.println(p2.country);
}
}

使用p1.country = “民国”;不推荐这样使用静态变量,推荐使用类名去访问静态的内容;

特点:

  • 数据共享
  • 属于类的,并不属于对象
  • 优先于对象产生的
    • 创建对象的过程(简单):
      1. 静态构造器;
      2. 通用构造器;
      3. 构造方法–>创建对象;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class test {
// 这里会报错,因为对象是在静态之后产生的,无法调用
// public static void test(){
// System.out.println(this.name);
// }
{
System.out.println("通用构造器");
}
static{
System.out.println("静态构造器");
}
public test(){
System.out.println("构造方法");
}
public static void main(String[] args) {
new test();
}
}

输出证明了是先执行静态构造器,而后执行通用构造器,最后执行构造方法;

由于创建对象的过程在静态内容加载完成之后,所以在静态方法和静态块里不能使用this,静态的内容使用类名去访问;

包和导包

包:本质上就是文件夹

在代码中需要写package 包名;

导包:

  • import 包+类;(import 包*–>会导入包的所有类);

不需要导包:

  • 在自己的包里;
  • java.lang包下所有的内容都不需要导包(String、System.out.println等);

访问权限

  1. public 公共的,所有人都可访问;
  2. default 包访问权限,在自己的包内可以随意访问;
  3. private 私有的;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package code;// 包
public class test {
public String pub = "public";
private String pri = "private";
String def = "default";

public static void main(String[] args) {
test t = new test();
System.out.println(t.pub);// 公共的,任何地方都可以访问
// System.out.println(t.pri);// 私有的,只有自己可以访问
// 这里不注释掉的话会报错,无法访问到私有的;
System.out.println(t.def);// 包访问的,只能在这个包里访问
}
}

getter和setter

成员变量一般使用private来声明,保护成员变量不被胡乱的赋值;

setter:主要是给成员变量赋值,做一定的保护;

getter:从成员变量中获取数据;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package code;

public class Persion {
private String name;
private int age;

// setter
public void setName(String name) {
this.name = name;
}
// 有效的保护我们的成员变量不被胡乱的赋值
public void setAge(int age) {
if (age > 0){
this.age = age;
} else{
this.age = 0;
}
}

// getter
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
public void chi() {
System.out.println(name + "正在吃饭");
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package code.System;
import code.Persion;

public class testPersion {
public static void main(String[] args) {
Persion p = new Persion();
// p.name("张三");
// p.age(-1);// 语法没有问题,但是逻辑上有问题;所以引入了setter,以便于设置限制

// 设置此人的信息,setter
p.setName("张三");
p.setAge(-1);

// 获取此人的信息,getter
System.out.println(p.getName());
System.out.println(p.getAge());
p.chi();
}
}

注意:idea里面右键点击“生成”可以一键生成“setter”、“getter”,微调限制即可;

继承

继承:子类可以自动拥有父类中除了私有内容外的其他所有内容;

当出现x是一种y的时候,可以使用x继承y;

public class 类 extends 父类{

}

1
2
3
4
5
6
7
8
9
10
11
12
package code.monster;

public class YaoGuai {
String name;
private void PaSi(){
System.out.println("妖怪怕死");
}
public void ChiRe(){
System.out.println("妖怪吃人");
}
}

父类:妖怪

1
2
3
4
5
package code.monster;

public class HeiXiongGuai extends YaoGuai {

}

子类:黑熊怪

1
2
3
4
5
6
7
8
9
10
11
12
13
package code.monster;

public class Test {
public static void main(String[] args){
HeiXiongGuai hx = new HeiXiongGuai();
hx.name = "大黑熊";
System.out.println(hx.name);

hx.ChiRe();
// hx.PaSi();// 私有内容,不能继承和访问
}
}

对代码进行测试;

下面用一个登陆操作解释说明:

1
2
3
4
5
6
7
8
package code.DengLu;

public class User {
public static void login(){
System.out.println("我要登录");
}
}

无论是Member还是Admin,都需要登陆功能,所以我们将二者合为一个,都继承User的登陆功能;

1
2
3
4
5
6
7
8
package code.DengLu;

public class Member extends User {// 普通账号
// public static void main(String[] args) {
// System.out.println("我要登陆");
// }
}

1
2
3
4
5
6
7
8
package code.DengLu;

public class Admin extends User {// 管理员账号
// public static void main(String[] args) {
// System.out.println("我要登陆");
// }
}

测试是否可行;

1
2
3
4
5
6
7
8
9
10
11
12
package code.DengLu;

public class Test {
public static void main(String[] args) {
Member m = new Member();
Member.login();

Admin a = new Admin();
Admin.login();
}
}

作用:

  • 简化代码的开发;
  • 子类对父类进行了扩展

super关键字

super:表示父类中的内容;

this:表示自己类中的内容;

用super和this来区分父类和子类中重名的内容;

1
2
3
4
5
6
7
8
9
10
11
package code.hero;

public class Hero {
String name = "孙悟空";

public Hero(String name){
this.name = name;
System.out.println("这是" + this.name + "构造方法");
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package code.hero;

public class SunWuKong extends Hero {
String name = "至尊宝";

public SunWuKong(){
// super();// Java赠送,默认的
super("父类");// 如果父类传递参数,这里就必须写出来,因为默认形式是super();是没有传入参数的
// 调用父类构造方法,这一行Java默认的,可以不写,这里写出来属于还原情况
// 在子类构造方法的第一行,默认调用父类的构造方法
System.out.println("这是子类构造方法");
}

public void chi(){
// 变量查找的顺序,先找自己类,然后找父类
System.out.println(this.name + "在吃桃子");
// 想看到父类中的name
System.out.println(super.name + "在吃桃子");
}
public static void main(String[] args){
SunWuKong swk = new SunWuKong();
swk.chi();
}
}

总结:

  • super可以获取到父类中的内容;
  • 可以调用父类中的构造方法,必须写在子类构造方法的第一行,如果父类的构造是无参的,可以不写,如果父类中有参,必须要写super;

方法的重写

重写:子类对父类中提供的方法进行重新定义;

语法:子类和父类中的方法的声明完全一致;

重写又被称为方法的覆盖;

1
2
3
4
5
6
7
8
package code.DaTang;

public class LiYuan {
public void MakeCountry(){
System.out.println("李渊想要建立自己的国家");
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package code.DaTang;

public class LiShiMin extends LiYuan {
// 子类中的方法必须和父类中的方法完全一致
public void MakeCountry(){
// 半盖,修改了子类继承父类的部分
super.MakeCountry();// super可以调用父类中被重写的方法
System.out.println("李世民想要建立自己的国家");
}
public static void main(String[] args){
LiShiMin lsm = new LiShiMin();
lsm.MakeCountry();
}
}

多态

多态:同一个对象拥有多种形态;

作用:把不同的数据类型进行统一,让程序具有超强的可扩展性;

知识点:

  • 把子类的对象赋值给父类的变量–>向上转型;(猫咪一定是动物,可以将猫咪当作动物来看待);
    • 缺点:屏蔽掉子类中特有的方法;
  • 把父类的变量转换为子类的变量–>向下转型 ;(但是动物不一定都是猫咪,所以向下转型有风险);
    • 向下转型可能有风险,Java要求必须要写强制类型转换;(转换之后的数据类型)变量;
1
2
3
4
5
6
7
8
9
10
11
12
13
package code.Animal;

// 猫是一种动物 --> 继承
// 可以把猫咪当作动物来看
public class Cat extends Animal{
public void eat() {
System.out.println("猫吃鱼");
}
public void catchMouse() {
System.out.println("猫抓老鼠");
}
}

1
2
3
4
5
6
7
8
package code.Animal;

public class Dog extends Animal{
public void eat() {
System.out.println("狗吃骨头");
}
}

1
2
3
4
5
6
7
8
package code.Animal;

public class DaXiang extends Animal{
public void eat() {
System.out.println("大象吃香蕉");
}
}

以上三种方法均合并为一种,继承自Animal;此处就是利用了多态将Cat、Dog、DaXiang都整合为了动物,都具有吃的功能,但同时猫咪看作动物也失去了抓老鼠的能力;

1
2
3
4
5
6
7
8
package code.Animal;

public class Animal {
public void eat() {
System.out.println("Animal is eating");
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package code.Animal;

public class Person {
public void feed(Animal a){// 接收到的所有动物都是Animal
a.eat();
}
// 每加一个动物就要给人多加一种饲养技能,太过麻烦
// public void feedCat(Cat c){
// c.eat();
// }
// public void feedDog(Dog d){
// d.eat();
// }
// public void feedDaXiang(DaXiang dx){
// dx.eat();
// }
}

在这里,三个方法合为一种后,人只需要会喂食动物即可;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package code.Animal;
public class Test {
public static void main(String[] args){
// Cat c = new Cat();
// Dog d = new Dog();
//
// Person p = new Person();
// p.feedCat(c);
// p.feedDog(d);
// Cat c = new Cat();// 创建一只猫咪
Animal ani1 = new Cat();// 可以把猫当成动物来看,把子类的对象赋值给父类的引用(变量),向上转型,会屏蔽掉子类中特有的方法(猫抓老鼠)
Animal ani2 = new Dog();
Animal ani3 = new DaXiang();

// ani1.catchMouse();// 会报错,因为站在动物的角度是不能抓老鼠的
Person p = new Person();
p.feed(ani1);
p.feed(ani2);
p.feed(ani3);

// 多态:把不同的数据类型进行统一

// 向下转型
Cat c = (Cat)ani1;
c.catchMouse();// 猫又可以抓老鼠了
}
}

final

  • 被final修饰的变量不可以被改变,又被称为常量;
  • 被final修饰的方法不可以被重写;
  • 被final修饰的类不可以被继承;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package code.Diamonds;

public final class Diamonds {
final int weight = 10;// 10克拉的钻石

public final void bling(){
System.out.println("bling bling");
}

public static void main(String[] args) {
Diamonds d = new Diamonds();
// d.weight = 5;// 会报错,final修饰的数据无法修改
System.out.println(d.weight);
}
}

1
2
3
4
5
6
7
8
9
10
11
package code.Diamonds;

// 被final修饰的类无法被继承
//public class PinkDiamonds extends Diamonds {
// 当public void bling()被final修饰后,无法被重写
// public void bling(){
// System.out.println("PinkDiamonds also bling bling");
// }

//}

总结:final是最终式,不可变;

抽象

抽象:现实中不存在的东西;

在Java中:只声明,不实现;

1
2
3
4
5
6
7
package code.Animal;

public abstract class Animal {// 类中如果有抽象方法,那么这个类必须是一个抽象类
//abstract修饰方法,这个方法就是一个抽象方法,抽象方法没有方法体,直接;结束
public abstract void eat();
}

抽象方法:使用abstract来修饰,不可以有方法体,直接用分号结束即可;

抽象类:如果一个类中有抽象方法,这个类必须是一个抽象类;

特点:

  • 抽象类不可以创建对象;
  • 抽象类的子类,必须重写父类中的抽象方法;否则,子类必须也是抽象类;
  • 通过抽象类可以强制的要求子类中必须有哪些方法;
1
2
3
4
5
6
7
8
9
10
11
12
package code.Animal;

public abstract class Animal {// 类中如果有抽象方法,那么这个类必须是一个抽象类
//abstract修饰方法,这个方法就是一个抽象方法,抽象方法没有方法体,直接;结束
public abstract void eat();
public abstract void move();
// 抽象类中可以有正常的方法
public void sleep(){
System.out.println("动物睡觉");
}
}

1
2
3
4
5
6
7
8
9
10
package code.Animal;
public class Cat extends Animal{
public void eat() {
System.out.println("猫吃鱼");
}
public void move(){
System.out.println("猫跑得很快");
};
// Animal ani = new Animal();// 会报错,Animal是抽象类,无法创建对象
}
1
2
3
4
5
6
7
8
9
package code.Animal;

public class Client {
public static void main(String[] args) {
Animal cat = new Cat();// 这里是多态性
cat.eat();
cat.move();
}
}

注:抽象类中可以有正常的方法

接口

接口:

  • 实际上是一种特殊的抽象类;
  • 中所有的方法都是抽象方法;
  • 使用interface来声明;
  • 同样具有多态性;
  • 可以把很多不相关的内容进行整合;
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* 能继承接口的只能是接口
* 接口和类只能是实现关系 implements
*/
package code.JieKou;

public interface Valuable{// 接口使用interface来声明,已经不再是类了
// 接口中的所有方法都是公共的,且均为抽象方法,不可以有正常的方法
// public abstract void getMoney();
// 所以此处可以省略public和abstract,与上文不省略的含义一样
void getMoney();
}

1
2
3
4
5
6
7
8
package code.JieKou;

public class Gold implements Valuable {
// 必须要实现抽象方法
public void getMoney() {
System.out.println("哇哦~是黄金,我们有救了");
}
}
1
2
3
4
5
package code.JieKou;

public interface Protectable {
public void protect();
}
1
2
3
4
5
6
7
package code.JieKou;

public class Animal {
public void eat() {
System.out.println("Animal is eating");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package code.JieKou;

public class Panda extends Animal implements Valuable,Protectable{
@Override
public void eat() {
System.out.println("Panda is eating 竹子");
}

@Override
public void protect() {
System.out.println("Panda is ChinaBaby you should protect it");
}

@Override
public void getMoney() {
System.out.println("Panda 花花是顶流");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package code.JieKou;

public class Test {
public static void main(String[] args) {
// Gold gold1 = new Gold();
// Valuable gold2 = new Gold();
// // 接口一样具有多态性
// gold1.getMoney();
// gold2.getMoney();
Panda p = new Panda();
Animal ani = new Panda();// 向上转型
Valuable val = new Panda();// 向上转型
Protectable pro = new Panda();// 向上转型

p.eat();
p.protect();
p.getMoney();
// 站在Panda的角度可以吃、受保护、作为顶流

ani.eat();
pro.protect();
val.getMoney();
// 站在Animal的角度只可以吃
// 站在Protectable的角度只可以受保护
// 站在Valuable的角度只可以成为顶流赚钱
}
}

注:类只能单继承,接口支持多实现;

特点:

  • 接口中所有的方法都是抽象方法,都是公开的;
  • 接口中所有的变量都是全局静态常量;

成员变量初始值

Java中所有的变量必须先声明,后赋值才能使用;

Java中的成员变量,在创建对象的时候,都会执行一次初始化操作,都会给一个默认值;

1
2
3
4
5
package code.ChuShiZhi;

public class Child {
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package code.ChuShiZhi;

public class Person {
byte b;
short s;
int i;
long l;
float f;
double d;
char c;
boolean bo;

String str;// String是Java提供的一个类 null
Child child;// 自己写的类 null
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package code.ChuShiZhi;

public class Test {
public static void main(String[] args) {
Person p = new Person();
System.out.println("byte="+p.b);
System.out.println("short="+p.s);
System.out.println("int="+p.i);
System.out.println("long="+p.l);
System.out.println("float="+p.f);
System.out.println("double="+p.d);
System.out.println("char="+p.c+" "+(int)p.c);
System.out.println("boolean="+p.bo);
System.out.println("String="+p.str);
System.out.println("Child="+p.child);
}
}

基本数据类型默认值都是0,包括Boolean–> false(0即为假);

引用类型:null,表示空,什么都没有,用来占位的;

object

万事万物皆为对象,所有的东西都是对象;

在Java中所有的类都要继承object,object是一个类,所有类的根;

我们写的类即使不写继承关系,默认也会继承object;

此处的student里面什么都没写,但是仍会有许多的对象,这些就是继承了object获得的;

equals和==

==判断左右两端的数据是否一致;

equals:object类提供的一个方法,用来判断两个对象是否相等(?????);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package code.Object;

public class Cat {
String name;
String color;

public Cat(String name, String color) {
this.name = name;
this.color = color;
}

public boolean equals(Cat c) {// 重写equals方法,从毛发颜色去写判断的逻辑
if(this.color==c.color){
return true;
}else{
return false;
}
}

public static void main(String[] args) {
Cat cat1 = new Cat("小花", "白色");
Cat cat2 = new Cat("小花", "白色");

// System.out.println(cat1 == cat2); // false 默认判断的是两个对象的内存地址是否一致,一般用在基本数据类型上
// System.out.println(cat1.equals(cat2)); // false 默认调用的是object提供的equals的方法
System.out.println(cat1.equals(cat2)); // true,此时调用的是自己重写的equals方法
}
}

两者用法相似,唯一的是equals可以自己重写;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package code.Object;

public class Test {
public static void main(String[] args) {
String str1 = "小红";
String str2 = "小红";

System.out.println(str1 == str2);// true
System.out.println(str1.equals(str2));// true
// String类除外,Java为了节省空间,当发现内存地址里面有小红这个字符串后,第二次的小红会直接将str2指向小红所在的内存地址,所以二者的内存地址也相同

String str3 = new String("小白");
String str4 = new String("小白");
System.out.println(str3 == str4);// false
System.out.println(str3.equals(str4));// true 判断两个字符串的内容是否一致

// 字符串的判断必须用 equals 可以帮我们判断内容
}
}

画板

字符串的判断一定要用equals来判断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package code.Object;

import java.util.Scanner;

public class Test2 {
public static void main(String[] args) {
String username = "LikeYou";
String password = "MeToo";

Scanner input = new Scanner(System.in);
System.out.println("请输入用户名(LikeYou):");
String username1 = input.nextLine();
System.out.println("请输入密码(MeToo):");
String password1 = input.nextLine();

if (username.equals(username1) && password.equals(password1)) {
System.out.println("Wo wo 登录成功!");
} else {
System.out.println("用户名或密码错误!");
}
}
}

_toString

toString()对一个对象的字符串的表示形式;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package code.ToString;

import com.sun.jdi.PathSearchingVirtualMachine;

public class Cat {// 类默认继承Object
String name;
String color;

public Cat(String name, String color) {
this.name = name;
this.color = color;
}

// 子类对父类提供的方法不满意,重写


@Override
public String toString() {
return "Cat{" +
"color='" + color + '\'' +
", name='" + name + '\'' +
'}';
}

public static void main(String[] args) {
Cat cat = new Cat("小白小白", "白色");

// 直接打印cat
System.out.println(cat);
// 默认打印对象,自动的执行这个对象中的 toString() 方法,继承自Object
System.out.println(cat.toString());
// 输出均为 code.ToString.Cat@b4c966a
// 默认的同String() --> 包+类名+@+内存地址
}
}

重写前的输出:–> 包+类名+@+内存地址;

Java官方推荐重写,可以右键“生成”来重写toString();

重写后如下图:

instanceof关键字

instanceof:判断xxx对象是否是xxx类型的

1
2
3
4
5
6
package code.GuanJianZi;

public class Animal {

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package code.GuanJianZi;
public class Cat extends Animal {
public static void main(String[] args) {
Animal ani1 = new Cat();
Animal ani2 = new Animal();

if(ani1 instanceof Cat){
System.out.println("是猫,那只猫就像她主人那么可爱");
}else {
System.out.println("不是猫,我的狗像我,我像我的狗");
}
System.out.println("========================");
if(ani2 instanceof Cat){
System.out.println("是猫,那只猫就像她主人那么可爱");
}else {
System.out.println("不是猫,我的狗像我,我像我的狗");
}
}
}

内存分析

  1. 堆:主要存放对象;
  2. 栈:局部变量,以及基本上数据类型的变量;
  3. 代码区:类和方法;
  4. 数据区:常量池和静态变量;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package code.NeiCunFenXi;

public class Persion {
private String name;
private int age;
private String address;

public static int num = 0;

public Persion(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;

num++;
}

public static void main(String[] args) {
int a = 10;
Persion p1 = new Persion("大乔", 18, "北京");
Persion p2 = new Persion("小乔", 19, "上海");
Persion p3 = new Persion("孙策", 20, "广州");
Persion p4 = new Persion("周瑜", 21, "深圳");
System.out.println("num = " + Persion.num);

}
}

画板

参数传递的问题

值传递:把变量的值作为参数进行传递;

引用传递:直接把变量作为参数进行传递;

Java使用的是值传递;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package code.ChuanDiWenTi;

public class Test {
public static void change(int b){
b = 20;
}
public static void main(String[] args) {
int a = 10;
change(a);

System.out.println("a的值为:"+a);
// 输出的a为10,将a的数据传到change中,使得b=10,之后在4行更改b=20;跳出change后会杀死进程,b也就没了,所以a的值未改变
}
}

输出的a为10,将a的数据传到change中,使得b=10,之后在4行更改b=20;跳出change后会杀死进程,b也就没了,所以a的值未改变

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package code.ChuanDiWenTi;

public class Test1 {

public static void change(Cat c){
c = new Cat("小白");
}

public static void main(String[] args) {
Cat c = new Cat("花花");
change(c);
System.out.println("cat的名字为:"+c.name);
}

}

总结

  1. 类与对象 –> 封装
  2. 构造
  3. 访问权限
  4. 继承
  5. 多态
  6. 抽象
  7. 接口
  8. 内存简单分析

异常处理

本质

异常就是错误对象;

  • 编译时异常;
  • 运行时异常;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package code.YiChangChuLi001;

public class Test {
public static void main(String[] args) {
System.out.println(1/0);// 0不能做分母
System.out.println("Wa wa");

// asdfghjkl// 无法识别,编译时异常
// 1.异常是错误,运行时异常
// 2.抛异常,创建一个错误对象,把错误对象丢出来
// 3.捕获异常,默认由JVM来把错误信息进行捕获,打印出来,JVM会终止程序的运行
}
}

抛异常

捕获异常,默认由JVM来捕获异常,程序会中断;

分类

画板

  1. RuntimeException,运行时异常(一般不手动处理,出问题了再处理);
  2. 其他Exception(必须要经过手动处理);
  3. Error(一般指的是系统级错误);

处理-try…catch

try{

尝试执行的代码

}catch(Exception e){

处理异常的代码

}finally{

最终的 

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package code.YiChangChuLi001;


public class Test1 {
public static void main(String[] args) {
try {
System.out.println(1 / 0);
} catch (Exception e) {
// e.printStackTrace();// 打印错误信息
System.out.println("系统出错了,请联系管理员");
}finally{// 一般做收尾工作
System.out.println("程序结束");
}

System.out.println("===================");

try {
System.out.println(1 / 1);
} catch (Exception e) {
// e.printStackTrace();// 打印错误信息
System.out.println("系统出错了,请联系管理员");
}finally{// 一般做收尾工作
System.out.println("程序结束");
}
}
}

异常的处理-throws和throw

throws表示方法准备要扔出来一个异常;

产生的错误尽可能的自己处理,少向外抛出异常;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

//public class Test2 {
// public static void read1() {
// try{
// InputStream is = new FileInputStream(new File("早安"));
// }catch(Exception e){
//
// }
// }
//
// public static void read2() throws Exception{// 表示当前这个方法会扔出来Exception这样的错误
// InputStream is = new FileInputStream(new File("早安"));
// }

public static void ChuFa(int a,int b) throws Exception{
if(b==0){
throw new Exception("除数不能为0");
} else {
System.out.println(a/b);
}
}

public static void main(String[] args) throws Exception {
// try {
// read2();
// } catch (Exception e) {
// throw new RuntimeException(e);
// }
ChuFa(1,2);
System.out.println("======");
ChuFa(1,0);
}

throw表示向外抛出异常;

自定义异常

直接继承Exception或者RuntimeException来实现自定义异常(非特定要求别使用);

1
2
3
4
5
6
7
8
9
10
11
package code.YiChangChuLi001;

public class Person {
String name;
String sex;
public Person(String name,String sex){
this.name = name;
this.sex = sex;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
package code.YiChangChuLi001;

public class ZaoTang {
public void man(Person p) throws Exception {
if(p.sex.equals("男")){
System.out.println("欢迎光临,贵宾一位");
}else{// 如果性别不是男同志
// 需要抛出异常
throw new Exception("唔???这里是男澡堂");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
package code.YiChangChuLi001;

public class Test3 {
public static void main(String[] args) throws Exception {
Person p1 = new Person("猪猪侠","男");
Person p2 = new Person("小菲菲","女");

ZaoTang z1 = new ZaoTang();
z1.man(p1);
z1.man(p2);
}
}