面向过程和面向对象的对比分析:
面向过程:步骤清晰简单,第一步做什么,第二步做什么。适合处理一些较为简单的问题。
举例——去吃饭:起身,走去餐桌,坐下,拿起筷子,吃。
那建高楼呢?面向对象就很难去分析了,因为太复杂了。
所以面向对象登场!
举例——建高楼:首先,找个总设计师,为每组分配一些任务,几个人做地基,几个人装门,几个人刷墙等等。然后每个组再细分任务给个人。
面向对象 其实是一种分类的思维模式,对于一个具体的问题,首先会想,解决问题需要哪些分类,然后对这些分类进行单独思考。最后,才对某个分类下的细节进行面向过程的思考。所以适合处理一些复杂的问题。
总的来说:面向对象和面向过程其实是不可分割的。
那么,什么是面向对象编程呢?本质就是:以类的方式组织代码,用对象来封装数据。
抽象的口头阐述:举个例子——建立一个学生管理系统:那么对于学生来说,只需要关心,学号,班级,姓名、成绩等。那么就得把他们的共同点抽出。
面向对象三大特性的口头阐述:
封装:一层层封起来,要用的时候再一级一级调用
继承:父类和子类
多态:比如工作,人去工作,那有人工作完成的好,有人完成的差。
btw:先有对象后有类。也就是说:类就是抽象的,对象是具体的。
复习一下:
import java.io.IOException;
//Demo01就是一个类,里边有个main方法
public class Demo01 {
public static void main(String[] args) {
}
public String sayHello(){
return "hello,world";
}
public void print(){
return;
}
//比大小也是个方法
public int max(int a, int b){
return a > b ? a : b;
}
//抛出异常
public void readFile(String file) throws IOException{
}
}
静态方法:
public class Student2 {
public static void say(){
System.out.println("学生说话了");
}
}
public class Demo02 {
//静态方法static
public static void main(String[] args) {
Student2.say();
}
}
不是静态方法了,则需要先实例化。
public class Demo02 {
public static void main(String[] args) {
Student2 student2 = new Student2();
student2.say();
}
}
public class Demo03 {
public static void main(String[] args) {
Demo03 demo03 = new Demo03();
System.out.println(demo03.add(3,5));
System.out.println(add2(3,5));
}
public int add(int a, int b){
return a+b;
}
public static int add2(int a, int b){
return a+b;
}
}
值传递和引用传递:
//值传递
public class Demo04 {
public static void main(String[] args) {
int a = 1;
System.out.println(a);
Demo04.change(a);
System.out.println(a);
}
//返回值为空
public static void change(int a){
a = 10;
}
}
//引用传递
public class Demo05 {
public static void main(String[] args) {
Person person = new Person();
System.out.println(person.name); //null
Demo05.change(person);
System.out.println(person.name); //ouou
}
public static void change(Person1 person){
person.name = "ouou";
}
}
//定义一个person类,有一个属性:name
class Person{
String name; //默认值为null
}
类和对象的关系:
动物、植物、、、、
张三、旺财、、、、
类和对象的创建:
public class Student {
String name;
int age;
public void study(){
System.out.println(this.name + "在学习");
}
}
public class Application {
public static void main(String[] args) {
//类是抽象的,需要实例化.
//实例化后会返回一个自己的对象
Student xiaoming = new Student();
Student xiaohong = new Student();
xiaoming.name = "小明";
xiaoming.age = 6;
xiaohong.name = "小红";
xiaohong.age = 6;
System.out.print(xiaoming.name+ " " + xiaoming.age);
System.out.println();
System.out.print(xiaohong.name+ " " + xiaohong.age);
}
}
构造器的详情
public class Person {
String name;
//一个类即使什么都不写,都会存在一个方法,也就是无参构造器
//实例化初始值
//1.使用new关键字,本质是在调用构造器
public Person(){
// this.name = "ouou"; // 有有参了后这儿就空着
}
//有参构造
public Person(String name){
this.name = name;
}
}
public class Application {
public static void main(String[] args) {
//new 实例化了一个对象
// Person person = new Person();
Person person = new Person("ouou");
System.out.println(person.name);
}
}
构造器的特点:
-
方法名和类名相同
-
没有返回值
构造器的作用:
-
new本质在调用构造器
-
初始化对象值
需要注意的点:
- 定义了有参构造后,如果要使用无参构造,就要定义一个显示的无参构造
public class Student {
//属性私有
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
if(age>120 || age<0){
this.age = 3;
}else{
this.age = age;
}
}
}
public class Application {
public static void main(String[] args) {
Student s = new Student();
s.setAge(130);
System.out.println(s.getAge());
}
}
封装的意义:
- 提高程序的安全性,保护数据
- 隐藏了代码的实现细节
- 统一了接口(get,set)
- 提高了系统的可维护性
2 继承
继承的本质是对一批类的抽象,从而实现对现实世界更好的建模。
继承是类和类之间的一种关系。继承关系的两个类,一个为子类,一个为父类。子类继承父类,用extends来表示,子类继承父类,就会拥有父类的全部方法。也就是说,子类和父类,从意义上来说,具有“is a”关系。
ctrl+h:可以查看继承树
需要注意的是:
- Java中类只有单继承,没有多继承!
- Java中所有的类都默认继承Object类
public class Person {
public int money = 10_0000_0000;
private int money2 = 10;
public int getMoney2() {
return money2;
}
public void setMoney2(int money2) {
this.money2 = money2;
}
public void say(){
System.out.println("说了一句话");
}
}
public class Student extends Person {
public static void main(String[] args) {
Student student = new Student();
student.say();
System.out.println(student.money);
// System.out.println(student.money2); 私有的不可继承
System.out.println(student.getMoney2());
}
}
public class Teacher extends Person {
}
2.1 super
public class Person {
protected String name = "ouyang";
}
public class Student extends Person {
private String name = "ouou";
public void test(String name){
System.out.println(name);
System.out.println(this.name);
System.out.println(super.name); //父类
}
}
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.test("欧阳");
}
}
输出:
欧阳
ouou
ouyang
默认调用父类的无参构造后,调用子类的无参构造。
2.2 super和thispublic class Person {
protected String name = "ouyang";
public void print(){
System.out.println("person");
}
}
public class Student extends Person {
private String name = "ouou";
public void test(String name){
System.out.println(name);//ou
System.out.println(this.name);//ouou
System.out.println(super.name);//ouyang
}
public void print(){
System.out.println("student");//student
}
public void test1(){
print();
this.print();
super.print();
}
}
public class Application {
public static void main(String[] args) {
Student student = new Student();
student.test("ou");
student.test1();
}
}
注意点:
- super调用父类的构造方法,必须在构造方法的第一个
- super必须只能出现在子类的方法或者构造方法中
- super和this不能同时调用构造方法
Vs this:
-
代表的对象不同:
this:对本身对象的调用
super:父类对象的引用
-
前提:
this:没有继承也能使用
super:只有在继承条件下才能使用
-
构造方法:
this():本类的构造
super():父类的构造
public class A {
public void test(){
System.out.println("A=>test");
}
}
public class B extends A {
@Override
public void test() {
System.out.println("B=>test");
}
}
注意事项:
- 需要有继承关系,子类重写父类的方法。
- 重写都是方法的重写,与属性无关
- 方法名必须相同,但方法体不同。
- 参数列表必须相同。
- 修饰符范围可以扩大,不能缩小。public>protected>default>private
- 抛出的异常:范围可以被缩小,但不能扩大:classNotFoundException —> Exception(大)
为什么需要重写呢?因为:父类的功能子类不一定需要,或者不一定满足。
3 多态父类:
public class Person {
public void run(){
System.out.println("run");
}
public void eat(){
System.out.println("eat");
}
private void say(){
System.out.println("say");
}
}
子类:
public class Student extends Person {
@Override
public void run() {
System.out.println("runrun");
}
public void sleep(){
System.out.println("sleep");
}
}
主启动类:
public class Application {
public static void main(String[] args) {
//一个对象的实际类型是确定的
new Person();
//可以指向的引用类型是不确定的;父类的引用指向子类
//student能调用的方法都是自己的或者继承父类的
Student s1 = new Student();
//person可以指向子类,但不能调用子类的方法
Person s2 = new Student();
Object s3 = new Student();
//子类重写了父类的方法,所以执行子类的方法
s1.run();//runrun
s2.run();//runrun
s2.eat();//eat
s1.sleep();//sleep
((Student) s2).sleep(); //sleep 强制转换
}
}
注意事项:
- 多态是方法的多态,属性没有多态。
- 父类和子类,有联系。不能不同的类型乱转换,会出现类型转换异常!ClassCastException
- 存在条件:继承关系,方法需要重写,父类的引用指向子类的对象。Father f1 = new Son();
- static方法属于类,不属于实例。不能被重写。
- final、常量、private不能被重写。
父类:
public class Person {
}
子类1:
public class Teacher extends Person {
}
子类2:
public class Student extends Person {
}
主启动类:
public class Application {
public static void main(String[] args) {
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 Teacher); //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 Teacher); //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 Teacher); //编译报错
// System.out.println(student instanceof String); //编译报错
}
}
注意:
- 父类引用指向子类的对象
- 把子类转换为父类,向上转型
- 把父类转换为子类,向下转型,强制转换
- 方便方法的调用,减少重复的代码
父类:
public class Person {
public void run(){
System.out.println("run");
}
}
子类:
public class Teacher extends Person {
public void sleep(){
System.out.println("sleep");
}
@Override
public void run() {
System.out.println("runrun");
}
}
主启动类:
public class Application {
public static void main(String[] args) {
Person obj = new Teacher();
Teacher teacher = (Teacher)obj;
teacher.sleep();
Person person = teacher;
}
}



