OOP面向对象

 

目录

 

什么是面向对象

方法的回顾和加深

创建和初始化对象

封装(是对属性的封装)

继承

多态

接口

异常

异常的分类

异常的体系结构

 Error

 Exception

异常的处理机制

 异常经验总结


什么是面向对象

方法的回顾和加深

package com.zhou.oop.demo;

import java.io.IOException;

//类
public class Test01 {
   // main方法
    public static void main(String[] args) {

    }
    /*
     修饰符 返回值类型  方法名(){
     方法体
     return 返回值;
    }
    * */
    //return 结束方法 返回一个结果!
    public String sayHello(){
        return "hello world";
    }
    public void print(){
        return;
    }
    public int max(int x,int y){
        return x>y?x:y;
    }
   
    public void readFile (String file)throws IOException{

    }
}

 

创建和初始化对象

 

package com.oop.demo02;

public class Person {
   // 一个类即使什么也不写,它也会存在一个方法
   // 显示的定义构造器

    String name;
   // 1. 使用new关键字,本质是在调用构造器
   // 2. 用来初始化值
    public Person(){

    }
    //有参构造:一旦定义了有参构造,无参就必须显示定义
    public Person(String name){
        this.name=name;
    }
}


/*
public class Application {
    public static void main(String[] args) {

        Person person = new Person("zhouxinlong");
    }
}

 构造器:
 1.和类名相同
 2.没有返回值
 作用:
 1.new 本质在调用构造方法
 2. 初始化对象的值
 注意点:
 1.定义有参构造之后,如果想使用无参构造,显示定义一个无参的构造

 Alt+Insert

 this. 是指当前类的

*/

封装(是对属性的封装)

package com.oop.demo03;

public class Student {

    private String name;
    private int age;
    private char sex;
    private int id;

     // 提供一些可以操作这个属性的方法!
     // 提供一些public 的 get、set方法

    public String getName() {
        return name;
    }
    public void setName(String name){
        this.name=name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if(age>120 || age<0){
            this.age=1;
        }else {
            this.age=age;
        }

    }

    public char getSex() {

        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}
/*
     封装的意义:
 *   1.提高程序的安全性,保护数据
 *   2.隐藏代码的实现细节
 *   3.统一接口
 *   4.系统可维护增加了
 *

public class Application {
    public static void main(String[] args) {
        Student student = new Student();

        student.setName("周新龙");

        student.setAge(999);//不合法
        System.out.println(student.getName());
        System.out.println(student.getAge());
    }
}

 */

继承

super注意点:
  1.super调用父类的构造方法,必须在构造方法里面的第一个
  2.super必须只能出现在子类的方法或者构造方法中!
  3.super和this不能同时调用构造方法!

VS this:
   代表的对象不同:
      this: 本身调用者这个对象(谁调用指向谁)
      super: 代表父类对象的应用
   前提
      this: 没有继承也可以使用
      super: 只能在继承的前提下才可以使用
   构造方法
      this() 本类的构造
      super() 父类的构造



重写:需要有继承关系,子类重写父类的方法!
  1.方法名必须相同
  2.参数列表必须相同
  3.修饰符:范围可以扩大但不能缩小 public>protected>default>private
  4.重写都是方法的重写,和属性无关
  5.抛出的异常:范围,可以被缩小,但不可以扩大 ClassNotFoundException ===> Exception(大)


重写,子类的方法和父类必须一致;方法体可以不同

为什么需要重写:
  1.父类的功能,子类不一定需要,或者不一定满足
  Alt+Insert


多态
注意事项:
   1.多态是方法的多态,和属性没有关系
   2.父类和子类,有联系 类型转换异常!ClassCastException!
   3.存在条件:继承关系,方法需要重写,只能是父类引用指向子类对象! Father f1=new Son();

      1.static 方法,属于类,它不属于实例
      2.fianl 常量;
      3.private 方法;

多态

 

 Object object = new Student();


        //System.out.println(X instanceof Y);能不能编译通过!X的实际类型是不是Y得子类型
        
        System.out.println(object instanceof Student);
        System.out.println(object instanceof Object);
        System.out.println(object instanceof Person);
        System.out.println(object instanceof Teacher);
        System.out.println(object instanceof String);

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

        Person person = new Student();
        System.out.println(person instanceof Student);
        System.out.println(person instanceof Object);
        System.out.println(person instanceof Person);
        System.out.println(person instanceof Teacher);
       // System.out.println(person instanceof String); 编译报错


        System.out.println("============");
        Student student=new Student();

        System.out.println(student instanceof Student);
        System.out.println(student instanceof Object);
        System.out.println(student instanceof Person);
//        System.out.println(student instanceof Teacher);  编译报错
//        System.out.println(student instanceof String);   编译报错


        Person obj = new Student();
        obj.run();
        Student student=(Student)obj;
        student.walk();
//        ((Student)obj).walk();
    }
}
/*
*   1.父类引用指向子类的对象
*   2.把子类转换为父类,向上转型
*   3.把父类转换为子类,向下转型;强制转换
*   4.方便方法的调用,减少重复的代码!
* 

接口

 

接口作用:
    1.约束
    2.定义一些方法,让不同的人实现~  10----> 1
    3.接口中默认方法 public abstract
    4.常量都是 public static final
    5.接口不能被实例化,接口中没有构造方法
    6.implements可以实现多个接口
    7.必须要重写接口中的方法

异常

异常的分类

异常的体系结构

 Error

 Exception

异常的处理机制

package com.exception.lesson02;

//自定义异常类
public class MyException extends Exception {

    //传递数字>10;
    private  int detail;

    public MyException(int a) {
       this.detail=a;
    }

    //toString:异常的打印信息
    @Override
    public String toString() {
        return "MyException"+'{'+detail+'}';
    }
}
package com.exception.lesson02;

public class Test01 {

    //可能会出现异常的方法
    static void test(int a) throws MyException {
        System.out.println("传递的参数"+a);
        if(a>10){
            throw new MyException(a);
        }
        System.out.println("OK");
    }

    public static void main(String[] args) {
        try {
            test(11);
        } catch (MyException e) {
            System.out.println("MyException:"+e);
        }
    }
}

 异常经验总结

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值