类与对象
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 ; Car c = new Car (); 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 () { 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 + "颜色的车会飞" ); } public static void main (String[] args) { 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 ; public Car () { System.out.println("无参数的构造方法被调用了" ); } 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, 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 address; public Person (String name, String address) { this .name = name; this .address = address; } public static void main (String[] args) { Person p1 = new Person ("赵铁柱" , "八大胡同" ); Person p2 = new Person ("李小花" , "朝阳门" ); Person.country = "民国" ; System.out.println(p1.country); System.out.println(p2.country); } }
使用p1.country = “民国”;不推荐这样使用静态变量,推荐使用类名去访问静态的内容;
特点:
数据共享
属于类的,并不属于对象
优先于对象产生的
创建对象的过程(简单):
静态构造器;
通用构造器;
构造方法–>创建对象;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class test { { 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等);
访问权限
public 公共的,所有人都可访问;
default 包访问权限,在自己的包内可以随意访问;
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.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; public void setName (String name) { this .name = name; } public void setAge (int age) { if (age > 0 ){ this .age = age; } else { this .age = 0 ; } } 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.setName("张三" ); p.setAge(-1 ); 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(); } }
对代码进行测试;
下面用一个登陆操作解释说明:
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 {}
1 2 3 4 5 6 7 8 package code.DengLu;public class Admin extends User {}
测试是否可行;
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 ("父类" ); System.out.println("这是子类构造方法" ); } public void chi () { System.out.println(this .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(); 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) { a.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) { Animal ani1 = new Cat (); Animal ani2 = new Dog (); Animal ani3 = new DaXiang (); 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 ; public final void bling () { System.out.println("bling bling" ); } public static void main (String[] args) { Diamonds d = new Diamonds (); System.out.println(d.weight); } }
1 2 3 4 5 6 7 8 9 10 11 package code.Diamonds;
总结:final是最终式,不可变;
抽象 抽象:现实中不存在的东西;
在Java中:只声明,不实现;
1 2 3 4 5 6 7 package code.Animal;public abstract class Animal { public abstract void eat () ; }
抽象方法:使用abstract来修饰,不可以有方法体,直接用分号结束即可;
抽象类:如果一个类中有抽象方法,这个类必须是一个抽象类;
特点:
抽象类不可以创建对象;
抽象类的子类,必须重写父类中的抽象方法;否则,子类必须也是抽象类;
通过抽象类可以强制的要求子类中必须有哪些方法;
1 2 3 4 5 6 7 8 9 10 11 12 package code.Animal;public abstract class Animal { 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("猫跑得很快" ); }; }
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 package code.JieKou;public interface Valuable { 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) { Panda p = new Panda (); Animal ani = new Panda (); Valuable val = new Panda (); Protectable pro = new Panda (); p.eat(); p.protect(); p.getMoney(); ani.eat(); pro.protect(); val.getMoney(); } }
注:类只能单继承,接口支持多实现;
特点:
接口中所有的方法都是抽象方法,都是公开的;
接口中所有的变量都是全局静态常量;
成员变量初始值 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; Child child; }
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) { 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.equals(cat2)); } }
两者用法相似,唯一的是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); System.out.println(str1.equals(str2)); String str3 = new String ("小白" ); String str4 = new String ("小白" ); System.out.println(str3 == str4); System.out.println(str3.equals(str4)); } }
字符串的判断一定要用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 { 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 ("小白小白" , "白色" ); System.out.println(cat); System.out.println(cat.toString()); } }
重写前的输出:–> 包+类名+@+内存地址;
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 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的值未改变
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 9 10 11 12 13 14 package code.YiChangChuLi001;public class Test { public static void main (String[] args) { System.out.println(1 /0 ); System.out.println("Wa wa" ); } }
抛异常
捕获异常,默认由JVM来捕获异常,程序会中断;
分类
RuntimeException,运行时异常(一般不手动处理,出问题了再处理);
其他Exception(必须要经过手动处理);
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) { System.out.println("系统出错了,请联系管理员" ); }finally { System.out.println("程序结束" ); } System.out.println("===================" ); try { System.out.println(1 / 1 ); } catch (Exception e) { 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 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 { 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); } }