文章目录
第零章
认识面向对象
- 物以类聚,分类的思维模式。首先解决需要哪些分类
- 面向对象适合处理复杂问题,适合处理多人协作的问
- 对于描述复杂的事物,为了从宏观上把握,从整体上合理分析,我们需要使用面向对象的思路来分析整个系统。但是具体到细微,还是要归结到面向过程中。
面向对象编程(OOP)
- 以类的方式组织代码,以对象组织(封装)数据。
- 抽象
- 特性:封装,继承,多态
- 人的角度:类是抽象的,对象是具体的
- 机器的角度:先有类后有对象。类是对象的模板
static关键字
- 与类一起被加载
值传递和引用传递
-
值传递
a = 1; sout(a); //a=1 change(a){ a = 10; } sout(a); //a=1
-
引用传递
class Person{ String name; } public class Demo01 { public static void main(String[] args) { Person person = new Person(); person.name = "zhang"; System.out.println(person.name); // zhang change(person); System.out.println(person.name); // han changer(person.name); System.out.println(person.name); // han } public static void change(Person person){ person.name = "han"; } public static void changer(String name){ name = "han"; } }
类与对象的关系
class修饰的类就是对象的一个类,对象的模板称为它的类。
构造器
class Person{
String name ;
public Person(){
// 无参构造
// this,name = "name"; 初始化参数
}
//有参构造定义后,默认无惨构造将无效。使用时需要显示的定义,否则将报错。
public Person(String name){
this.name = name; //有参构造
}
}
简单内存分析
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3mMd3jlN-1635758834757)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\image-20211030093934223.png)]
小结
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8iPvFrnR-1635758834760)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\image-20211030094238335.png)]
封装
-
该漏的漏,该藏的藏。程序要求 高内聚,低耦合。
-
将属性私有化,防止外部用户私自使用。
-
使用外部接口(get/set)使用类内部的属性。
避免用户的错误操作,保护程序保护属性,隐蔽代码细节,提高系统的可维护性
public class Person { private String name; // 属性私有化 public Person() { // 无参构造 } public Person(String name) { this.name = name; // 有参构造 } public String getName() { //get方法 return name; } public void setName(String name) { //set方法 this.name = name; } }
继承
- 继承的本质是对某一批类的抽象,从而实现对现实世界更好的建模
- extends的意思是”扩展“,子类是父类的拓展
- JAVA中一个字类只有一个父类,一个父类可以用多个子类,只能单继承
- 继承是类与类只间的一种关系,此外还有依赖,组合,聚合等
- 继承关系中的两个类,一个为子类(派生类)一个父类(基类),子类继承父类使用关键字extends表示
Object类
- 所有的类都显示或隐式、直接或间接的继承Object类,其中定义了一些本地静态方法。
super和this关键字
-
super调用父类的被保护的属性和方法,this调用子类的属性和方法。
-
super():在子类中调用父类的无惨构造,必须写在构造器的第一个。
-
super():只能出现在子类的方法和构造其中。
-
super()&this()不能同时调用构造方法
属性 this super 对象 调用者本身 代表父类的应用 前提 没有继承可以使用 只用继承父类的情况下使用 构造方法 本类构(有参无参) 调用父类的构造(有参无参) public class Person { //父类 private String id; //属性私有化 protected String name; // 受保护的属性 public Person() { // 无参构造 } public Person(String name) { this.name = name; // 有参构造 } public Person(String id, String name) { //有参构造方法重载 this.id = id; this.name = name; } public String getId() { //get方法 return id; } public void setId(String id) { //set方法 this.id = id; } public String getName() { //get方法 return name; } public void setName(String name) { //set方法 this.name = name; } } public class Student extends Person{ //子类 //定义子类私有属性 private String ogj; //子类无惨构造 public Student() { super(); //调用父类无参构造 } //子类有参构造,方法重载 public Student(String name) { super(name); //调用父类有参构造 } //子类有参构造,方法重载 public Student(String name, String ogj) { super(name); this.ogj = ogj; } //子类有参构造,方法重载 public Student(String id, String name, String ogj) { super(id, name); this.ogj = ogj; } public String getOgj() { super.name = name; //拿到父类中受保护属性的参数 return ogj; } public void setOgj(String ogj) { this.ogj = ogj; } }
方法重写
-
重写仅限于方法的重写,改变扩充父类中的方法
-
子类和父类中重写的方法,再多态的加持下可以执行出不同的结果
-
@Override 重写的注解
-
重写普通方法覆盖父类中方法,不能重写的方法:static、final、private修饰的方法
-
方法的修饰范围可以了扩大,当产生异常时异常可以缩小
-
public void test(){ System.out.println("父类中的方法"); } public static void test2(){ System.out.println("父类中的静态方法"); } @Override public void test(){ System.out.println("子类重写父类中的方法"); } public static void test2(){ System.out.println("子类中父类的静态方法"); //不能重写 }
多态
- 动态编译:让程序的类型更有可扩展性
- 即同一方法可以根据发送对象的不同采用多种不同的行为方式
- 一个对象的实际类型是确定的,但指向对象的引用的类型是有很多的
多态的存在条件
-
有继承关系
-
子类重写父类的方法
-
在父类定义中指向子类的引用
-
多态是方法的多态 实属性没有多态性
public class Person { public void run(){ System.out.println("父类在跑"); } } public class Student extends Person{ public void run(){ System.out.println("学生再跑"); } } public class Teacher extends Person{ @Override public void run() { System.out.println("老师再跑"); } } public class Demo { public static void main(String[] args) { Person person = new Person(); person.run(); //父类在跑 Student student = new Student(); student.run(); //学生再跑 Person person1 = new Student(); //向下转型 person1.run(); //学生再跑 Person person2 = new Teacher(); //向下转型 person2.run(); //老师再跑 } } /* 父类在跑 学生再跑 学生再跑 老师再跑 */
instanceof(类型转换)引用类型转换
Object object = new Student();
System.out.println(object instanceof Student); //true
System.out.println(object instanceof Person); //true
System.out.println(object instanceof Object); //true
System.out.println(object instanceof String); //false
Person person = new Student();
System.out.println(person instanceof Student); //true
System.out.println(person instanceof Person); //true
System.out.println(person instanceof Object); //true
// System.out.println(person instanceof String); //错误
System.out.println(person instanceof Teacher); //false
Student student = new Student();
System.out.println(student instanceof Student); //true
System.out.println(student instanceof Person); //true
System.out.println(student instanceof Object); //true
// System.out.println(person instanceof String); //错误
// System.out.println(student instanceof Teacher); //错误
两个类有关系则可以进行强制转换
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5nCn9Hoo-1635758834761)(C:\Users\DELL\AppData\Roaming\Typora\typora-user-images\image-20211030153708712.png)]
static介绍
- 静态变量 static int a;
- 静态方法 static void main(){ ………… }
- 静态代码块 { ………… }
- 静态导入包 import static com.lang.Math
- 可以直接调用,因为static修饰可以跟类一起加载
- 相比匿名代码块跟随方法的执行进行初始,常用语初始化
抽象类
- abstract修饰,进行约束某些类的实现
- 不能被创建,只能由子类实现或者子子类
- 抽象类中的方法子类必须全部实现,还可以加入其他方法
public abstract class Person { //抽象类
public abstract void run(); //抽象方法
public void dump(){
System.out.println("跳");
}
}
public class Student extends Person{ //实现类
@Override
public void run() { //实现抽象方法
System.out.println("跑");
}
}
public class Demo {
public static void main(String[] args) {
Student student = new Student();
student.run();
student.dump();
}
}
接口
- 普通类:只有具体实现
- 抽象类:具体实现和规范(抽象方法)都有
- 接口:只有规范!自己无法写方法–>专业的规范。约束和实现分离:面向接口编程
- 接口就是一组规范,本质是一组契约。不同的开发人员通过一样的接口进行合作开发
- 声明接口的关键字是interface,声明类的关键字是class
public interface Person { //接口1
void run();
}
public interface WorK { //接口2
void job();
}
public class Student implements Person,WorK{ //接口实现类
@Override
public void run() { //接口1实现方法
System.out.println("跑");
}
@Override
public void job() { //接口2实现方法
System.out.println("学生");
}
}
public class Demo {
public static void main(String[] args) {
Student student = new Student();
student.job();
student.run();
}
}
内部类
- 外部类(public class A)中的类(class B)
- 静态内部类(public static class A)
- 匿名内部类、局部内部类
public class Apple { //外部类
public static void main(String[] args) {
new Banana().eat(); //匿名对象
new fruit(){ //匿名内部类
@Override
public void pear() {
System.out.println("吃梨");
}
}.pear();
}
}
class Banana{ //内部类
public void eat(){
System.out.println("吃香蕉");
}
}
static class vegetable{ //静态内部类
public void eat(){
System.out.println("吃蔬菜");
}
}
interface fruit{ //内部接口
void pear();
}
异常机制
- 程序中也有不测风云,会有异常产生。进行意外处理的机制称为异常机制
- 程序运行中遇的问题
- 异常分类:Error和Exception(RuntimeException……)
- try 、catch 、finally、 throw、 throws 异常关键字
public class Exception {
public static void main(String[] args) {
int a = 2;
int b = 0;
try {
System.out.println(a/b);
} catch (java.lang.Exception e) {
// 加入处理异常的语句
e.printStackTrace(); //异常打印 java.lang.ArithmeticException: / by zero
} finally {
System.out.println("finally"); //善后执行操作
}
}
}
public static void test(){
int a = 2;
int b = 0;
if (b == 0){
throw new ArithmeticException(); //方法里主动抛出异常
}
System.out.println(a/b);
}
public static void test1() throws ArithmeticException{//方法上主动抛出异常,交给上层调用者
int a = 2;
int b = 0;
System.out.println(a/b);
}
public static void main(String[] args) {
try { //捕获产生的异常
test();
} catch (java.lang.Exception e) {
// 加入处理异常的语句
e.printStackTrace(); //异常打印 java.lang.ArithmeticException
} finally {
System.out.println("finally"); //善后执行操作
}
}
自定义异常
public class MyException extends Exception { //自定义异常类
private int a ;
public MyException(int a) {
this.a = a;
}
@Override
public String toString() {
return "MyException{" +
"a=" + a +
'}';
}
}
public class Test {
public static void main(String[] args) {
int a = 12;
try { //捕获产生的异常
test(a);
} catch (MyException e) {
e.printStackTrace(); //异常打印
}
System.out.println("ok"); //结束
}
public static void test(int a) throws MyException { //异常抛出方法
if(a>10){
throw new MyException(a);
}
}
}