- 面向对象和面向过程区分
- 面向过程思想:
- 布局清晰简单,第一步做什么,第二步做什么....
- 面向过程适合处理一些简单的问题
- 比如:把大象装进冰箱里需要做(反正要一步一步来)
- 第一步:把大象宰了
- 第二步:把冰雪们打开
- 第三步:把大象装进去
- 第四步:把冰箱门关起来
- 面向对象思想:
- 物以类聚,分类的思维模式,思考问题首先会解决问题需要哪些分类,然后对这些分类进行单独思考。最后,才对某个分类下的细节进行面向过程的思索。
- 面向对象适合处理复杂的问题,适合处理需要多人协作的问题!
- 比如:我可以分配一个人去宰大象,分配另一个人去开冰箱...(这里只是举例而已)
- 面向过程思想:
- 什么是面向对象
- 面向对象编程(Object-Oriented Programming,OOP)
- 面向对象编程本质就是:以类的方式组织代码,以对象的组织(封装)数据(以类的方式组织代码,以对象的方式封装数据)
- 抽象
- 三大特征:
- 封装
- 继承
- 多态
- 从认识论角度考虑是先有对象后有类。对象,是具体的事务。类,是抽象的,是对对象的抽象。
- 从代码运行角度考虑是先有类后有对象。类是对象的模板
- 复习
- 创建 Student
package com.oop.demo01; //学生类 public class Student { //静态方法 public static void say(){ System.out.println("学生说话"); } //非静态方法 public void say1(){ System.out.println("学生说话1"); } }
- 创建Demo01
package com.oop.demo01; public class Demo01 { //main 方法 public static void main(String[] args) { Demo01 demo01 = new Demo01(); int max1 =demo01.max(12,3); System.out.println(max1); } /* 修饰符 返回值类型 方法名(...){ //方法体 return 返回值; } */ public String sayHello(){ return "hello,word"; } //没有返回值,为空。 public void hello(){ return; } //实际参数类型和形式参数类型要一一对应 public int max(int a,int b){ return a>b ? a:b;//三元运输符 } }
- 创建Demo02
package com.oop.demo01; public class Demo02 { public static void main(String[] args) { //调用静态方法 Student.say(); //调用非静态方法 Student student = new Student(); student.say1(); } }
- 创建Demo03
package com.oop.demo01; import com.kuang.scanner.Dome3; public class Demo03 { public static void main(String[] args) { int a = 1; System.out.println(a);//1 change(20); Perosn perosn = new Perosn();//null System.out.println(perosn.name); Demo03.change1(perosn);//张三 System.out.println(perosn.name); } public static void change(int a){ a = 10; } public static void change1(Perosn perosn){ //perosn是一个对象:指向的 ---> Perosn perssn = new Perosn();这是一个具体的人,可以改变属性! perosn.name = "张三"; } } //定义一个Perosn类,有一个属性:name class Perosn{ String name;//null }
- 创建 Student
- 类与对象
- 类与对象的关系
- 类:类是一种抽象的数据类型,它是对某一事务的整体描述/定义,但并不能代表某一个具体的事务。
- 对象:对象是抽象概念的具体实例
- 创建与初始化对象
- 使用new关键字创建的时候,除了分配内存空间之外,还会给创建好的对象进行默认的初始化以及对类中构造器的调用
- 类中的构造方法,是在进行创建对象的时候不许要调用的。并且构造器有一下两个特点:
- 必须和类的名字相同
- 必须没有返回类型,也不能写void
- 类与对象的关系
- 封装,继承,多态
- 封装(数据的隐藏)该露的露,该藏得藏
- 我们程序设计要追求“高内聚,低藕合”。高内聚就是类的内部数据 *** 作细节自己完成,不允许外部干涉;低耦合:仅暴露少量的方法给外部使用
- 通常,应禁止直接访问一个对象中数据的实际表示,而通过 *** 作接口来访问,这称为信息隐藏。
- 记住这句话就够了:属性私有,get/set
- 代码:
- 创建Application
-
package com.oop.demo04; public class Application { public static void main(String[] args) { Student s1 = new Student(); s1.setName("张三"); System.out.println(s1.getName()); } }
- 创建Student
package com.oop.demo04; //类 private : 私有 public class Student { private String name; //姓名 private int id; //学号 private char sex; //性别 //提供一些可以 *** 作这个属性的方法! //提供一些public的get,set方法 //get 获得这个数据 public String getName() { return name; } //set 给这个数据设置值 public void setName(String name) { this.name = name; } }
-
- 创建Application
- 继承
- 继承的本质是对某一批类的抽象,从而实现对现实世界更好的建模
- extends的意思是“扩展”。子类是父类的扩展。
- Java类中只有单继承,没有多继承!(只能有一个爸爸)
- 继承类和类之间的一种关系。除此之外,类和类之间的关系还有依赖,组合,聚合等。
- 继承关系的两个类,;一个为子类(派生类),一个为父类(基类)。子类继承父类,使用关键字extends来表示。
- objext类
- super — this
- 代码:
- 创建Appliction
package com.oop.demo05; public class Appliction { public static void main(String[] args) { Student student = new Student(); student.say(); System.out.println(student.getMoney()); } }
- 创建:Student
package com.oop.demo05; //学生 is 人 : 派生类,之类 //子类继承了父类,就会拥有父类的全部方法! public class Student extends Person { }
- 创建:Person
package com.oop.demo05; public class Person { private int money = 20_0000_0000; public void say(){ System.out.println("说了一句话"); } public int getMoney() { return money; } public void setMoney(int money) { this.money = money; } }
- 创建Appliction
- 代码:
- 方法重写
- 代码(可以在原先的基础上该,也可以重新创建):
- 创建Appliction
package com.oop.demo07; //重写 public class Appliction { //静态方法和非静态方法的区别很大 //静态方法:方法的调用只和左边,定义的数据类型有关 //非静态:重写 public static void main(String[] args) { A a = new A(); a.test();//A //父类的引用指向子类 B b = new A(); b.test();//B } } /* */
- 创建B
package com.oop.demo07; public class B { public void test(){ System.out.println("B=>test()"); } }
- 创建A继承B
package com.oop.demo07; public class A extends B { //重写 //出现了右边的箭头才是重写了的 public void test(){ System.out.println("A=>test()"); } }
- 创建Appliction
- 代码(可以在原先的基础上该,也可以重新创建):
- 多态即同一方法可以根据发送对象的不同而采用多种不同的行为方式。
- 一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多
- 多态存在的条件
- 有继承关系
- 子类重写父类方法
- 父类引用指向子类的对象关键字
- 注意:多态是方法的多态,属性没有多态性
- instanceof (类型转换)引用类型
- 代码
- 创建Appliction
package com.oop.demo08; public class Appliction { public static void main(String[] args) { //一个对象的实际类型是确定的 //new Student(); //new Person(); //可以指向的引用类型就不确定了:父类的引用指向子类 //Student能调用的方法都是自己的或者继承父类的! Student s1 = new Student();//Student_son //Person 父类型,可以指向子类,但不能调用子类独有的方法 Person s2 = new Student();//Student_son Object s3 = new Student(); //对象能执行哪些方法,主要看对象左边的类型,和右边关系不大! s1.run();//子类重写父类的方法,执行的子类的方法 s2.run(); //Object > String //Object > Person > Teacher //Object > person > Student 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 Theacher);//false System.out.println(object instanceof String);//false System.out.println("========================="); 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 Theacher);//false // System.out.println(person instanceof String); 编译报错! System.out.println("========================="); 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(student instanceof Theacher); 编译报错 // System.out.println(student instanceof String); 编译报错! //类型之间的转换:父子 //高 //低 Person obj = new Student(); Student student1 = new Student(); //student将这个对象转换为Studnet类型,我们就可以使用Studnet类型的方法了!(强制转换) ((Student)obj).go(); //go方法是Student里面独有的方法 student.go(); Person person1 = student1; /* 1.父类引用指向子类的对象 2.把子类转换为父类的对象,向上转型 3.把父类转型为之类,向下转型:强制转型 4.方便方法的调用,减少重复代码!简介 */ } }
- 创建Person
package com.oop.demo08; public class Person { public void run(){ System.out.println("Person_run"); } } /* 多态注意事项: 1.多态是方法的多态,属性没有多态 2.父类和子类,有联系 类型转换异常! ClassCastException! 3.存在的条件:继承关系;方法需要重写;父类引用指向子类对象! Father f1 = new Son(); */
- 创建Student继承Person
package com.oop.demo08; public class Student extends Person { @Override public void run() { System.out.println("Student_son"); } public void eat(){ System.out.println("eat"); } public void go(){ System.out.println("Studnt中方法go方法"); } }
-
创建Theacher继承Person
package com.oop.demo08; public class Theacher extends Person{ }
- 创建Appliction
- 关键字“static”
- 理解fial关键字
- 代码:
- 创建Person类
package com.oop.demo09; /* final可以修饰变量,方法和类,用于表示所修饰的内容一旦赋值之后就不会再被改变,比如String类就是一个final类型的类。 即使能够知道final具体的使用方法,我想对final在多线程中存在的重排序问题也很容易忽略,希望能够一起做下探讨 */ public final class Person { //第二运行 用于赋初值 { //代码块(匿名代码块)非静态 System.out.println("非静态匿名代码块"); } //第一运行 只执行一次 static { //加载初始化 //代码块(匿名代码块)静态 System.out.println("静态匿名代码块"); } //第三运行 public Person() { System.out.println("构造方法"); } public static void main(String[] args) { Person person = new Person(); System.out.println("======================="); //再次运行静态代码块没了 Person person1 = new Person(); } }
- 创建Student类
package com.oop.demo09; //static : 静态 //因为加了final会报错如果要使用就在Person类中把Final去掉 public class Student extends Person { //静态变量只能在静态方法中访问 private static int age;//静态变量 多线程在用! private int name;//非静态变量 public static void main(String[] args) { //属性的使用 Student s1 = new Student(); System.out.println(Student.age); // System.out.println(Student.name);非静态变量不能这么写 System.out.println(s1.age); System.out.println(s1.name); //方法的使用 go(); s1.run(); } public void run(){ //非静态调用静态直接写方法名 go(); } public static void go(){ Student.go(); } }
- 创建Text类
package com.oop.demo09; //静态导入包 import static java.lang.Math.PI; import static java.lang.Math.random; public class Test { public static void main(String[] args) { System.out.println(Math.random()); System.out.println(PI); } }
- 创建Person类
- 封装(数据的隐藏)该露的露,该藏得藏
-
- super注意点:
- super调用父类构造方法,必须在构造方法的第一个
- super必须只能出现在子类的方法或者构造方法中!
- super和this泵同时调用构造方法!
- super和this的不同:
- 代表对象不同:
- this:本身调用者这个对象
- super:代表父类对象的应用
- 使用前提:
- this:没有继承也能用
- super:只能在继承条件下才能使用
- 构造方法:
- this():调用本类构造
- super():父类构造
- 代表对象不同:
-
重写:需要有继承关系,子类重写父类的方法 1.方法名必须相同 2.阐述列表列表必须相同 3.修饰符:范围可以扩大但不能缩小: public > protected > default > private 4.抛出的异常:范围,可以被缩小,但不能扩大: 重写,子类的方法和父类必要一致:方法体不同! 1.父类的功能,子类不一定需要,或者不一定满足! Alt + Insert : override;
- super注意点:
最后留言:
努力学习吧,成为更好的自己。
推荐视频:代码基本按照这样写的,大家一起学习!加油!!!狂神说Javahttps://www.bilibili.com/video/BV12J41137hu?p=74&spm_id_from=333.880.my_history.page.click
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)