JAVA面向对象

一、初识面向对象

  • 对于描述事物,为了从宏观什么把握、从整体上合理分析,我们需要使用面向对象的思路来分析整个系统。但是,具体到微观操作,仍然需要面向过程的思路去处理
  • 面向对象编程的本质就是:以类的方式组织代码,以对象的组织(封装)数据
  • 抽象:编程思想!持续的学习
  • 三大特性
    • 封装
    • 继承
    • 多态
  • 从认识论角度考虑先有对象后有类,是具体的事务。类,是抽象的,是对对象的抽象
  • 从代码运行角度考虑是先有类后有对象。类是对象的模板

二、方法回顾和加深

  • 方法的定义

    • 修饰符
    • 返回类型
    • break和return的区别
    • 方法名:注意规范 见名知意
    • 参数列表(参数类型,参数名)
    • 异常抛出:疑问,后面记录……
    //Dmeo01  类
    public class Dome01 {
        //main 方法
        public static void main(String[] args) {
    
        }
        /**
         * 修饰符 返回值类型 方法名(……){
         *     方法体
         *     return 返回值
         * }
         * */
        public String sayHello(){
            return "hello,world";
        }
    
        public  int max(int a ,int b){
            return a > b ? a : b;//三元运算符
        }
    
        public  void readFile(String file) throws IOException{
    
        }
    }
    
  • 方法的调用

    • 静态方法
    • 非静态方法
      public static void main(String[] args) {
            //静态方法
            Student.say01();
    
    
            //非静态方法
            //实例化这个类
            new Student().say02();
            //可以写成
            //对象类型 对象名 = 对象值
            Student student = new Student();
            student.say02();
    
        }
    
    //学生-类
    public class Student {
        //学生.说话-方法
    
    
        //静态方法01
        //和类一起加载的
        public static void say01() {
            System.out.println("学生说话了");
        }
    
        //非静态方法
        //类实例化之后才加载的
    
        public void say02(){
            System.out.println("学生说话了");
        }
      }
    
    • 形参和实参
    public static void main(String[] args) {
            //实际参数和形式参数的类型要对应
            //new Dome03().add(1,2);
            Dome03 dome03 = new Dome03();
            dome03.add(1,2);
            System.out.println(add);
        }
        public int add(int a,int b){
            return  a+b;
        }
    
    • 值传递和引用传递
    public class Dome04 {
        public static void main(String[] args) {
            //值传递
           int a = 1;
           System.out.println(a);
    
           Dome04.chang(a);
    
           System.out.println(a);
    
           //引用传递
            //实例化Perosn类 创建perosn对象
            Perosn perosn = new Perosn();
            System.out.println(perosn.name);
    
            Dome04.chang(perosn);
            System.out.println(perosn.name);
    
        }
        public  static  void  chang(int a ){
            a = 10;
        }
        public  static  void chang (Perosn perosn){
            perosn.name = "xiangSir";
        }
    }
    //定义了一个Perosn类,有一个属性:name
    class Perosn{
        String name;//null
    }
    
    • this关键字

三、对象的创建分析

  • 对象是类的实例
package com.oop.Dome02;
//一个项目应该只存一个main方法
public class Application {
    public static void main(String[] args) {
        //类:抽象的,实例化
        //类实例化后会返回一个自己的对象、
        //student对象就是一个Student类的具体实例
        Student student = new Student();
        student.stuay();


        student.name = "小米";
        student.age = 3;
        student.stuay();
    }
}

package com.oop.Dome02;
//学术类
public class Student {
    //属性:字段
    String name;
    int age;

    //方法
    public  void stuay(){
        System.out.println(this.name+"在学习");
    }
}

  • 构造器也成为构造方法,是在进行创建对象的上海必须要调用的,并且构造器有以下两个特点

    • 必须和类的名字相同
    • 必须没有返回类型,也不能写void
    package com.oop.Dome02;
    //一个项目应该只存一个main方法
    public class Application {
        public static void main(String[] args) {
            //new实例化一个对象
            Persom persom = new Persom();
            System.out.println(persom.name);
        }
    }
    
    package com.oop.Dome02;
    
    public class Persom {
    
        String name;
    
        //实力化初始值
        //1.使用new关键字,必须要有构造器
        //2.用来初始化值
        public Persom(){
            this.name = "xiangsir";
        }
        //有参构造:定义有有参构造,无参构造必须显式定义
        public Persom(String name){
            this.name= name;
        }
    
    //alt+inserl快速构造器
    
    }
    /*
        构造器:
        1.和类名相同
        2.没有返回值
        作用:
        1.new本质在调用构造方法
        2.初始化值
        注意点:
        1.定义有参构造之后,如果想使用无参构造,显示的定义一个无参构造
    
        alt+inserl快速构造器
    
    */
    

四、面向对象三大特征

4.1封装

  • 属性私有化 privte

  • 提供一些可以操作这个属性的方法

  • get读 set写

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

4.2继承

4.2.1object类

  • extends

  • 所有的类默认继承Object

  • 私有类不能被继承

  • 子类会继承父类的方法

  • package com.oop;
    
    import com.oop.Demo05.Person;
    import com.oop.Demo05.Student;
    
    //一个项目应该只存一个main方法
    public class Application {
        public static void main(String[] args) {
         Person person= new Person();
         person.say();
         System.out.println(person.getMoeny());
        }
    }
    
     
    package com.oop.Demo05;
    
    //public  继承一定要公开
    //private
    public class Person {
        private int moeny = 10_0000_0000;
        public void say(){
            System.out.println("梅新海在说话");
        }
        public int getMoeny(){
            return moeny;
    }
        public void setMoeny(int moeny) {
            this.moeny = moeny;
    }
    }
    
    
    
    package com.oop.Demo05;
    //学生 is 人  派生类,子类
    //子类集成了父类 就会拥有父类的全部方法
    public class Student extends Person{
        
    }
    
    
  • System.out.println(name);//输出传递过来的值
            System.out.println(this.name);//输出本类里面的值
            System.out.println(super.name);//输出父类里面的值 
    
  • 默认调用父类的无参构造 super()

  • 父类没有无参构造子类要有有参构造

4.2.2super AND this

  • super调用父类的构造方法,必须在构造方法的第一个
  • super必须只能出现在子类的方法或者构造方法中
  • super和this不能同时调用构造方法

vs this

  • 代表的对象不用:

    • this:本身调用者这个对象
    • super:代表父类对象的应用
  • 前提

    • this:没有继承也可以使用
    • super:只能在继承条件才可以调用
  • 构造方法

    • this():本类的构造
    • super():父类的构造
  • package com.oop;
    
    import com.oop.Demo05.Person;
    import com.oop.Demo05.Student;
    import com.oop.Demo05.Teacher;
    
    //一个项目应该只存一个main方法
    public class Application {
        public static void main(String[] args) {
       Teacher student = new Teacher();
        //student.test01();
        }
    }
    
     
    package com.oop.Demo05;
    //老师 is 人  派生类,子类
    public class Teacher extends Person{
    
    
        public Teacher(){
            System.out.println("Teacher子类的无参构造形成了");
        }
    
    
        private String name = "shitou" ;
    
        public void print(){
            System.out.println("student");
        }
        public void test01(){
            print();
            this.print();
            super.print();
        }
        public void test02(){
          
        }
    }
    
    package com.oop.Demo05;
    
    //public  继承一定要公开
    //private
    public class Person {
    
    
        public Person(){
            System.out.println("Person无参执行了");
        }
    
        protected String name = "xiang";
        public void print(){
          System.out.println("Person");
      }
    }
    

4.2.3重写

  • 非静态方法

  • 重写

    • 需要有继承关系,子类重写父类的方法
    1. 方法名必须相同

    2. 参数列表必须相同

    3. 修饰符:范围可以扩大 但是不能缩小

      public >Protected>default>privat

    4. 抛出的异常:范围,可以被缩小 但是不能扩大

      classNotFounfException -->Exception

  • 子类的方法和父类必须要一致

  • 重写原因

    1. 父类的功能,子类不一定需要,或者不一定满足
    2. override
    package com.oop;
    
    import com.oop.Demo05.A;
    import com.oop.Demo05.B;
    
    //一个项目应该只存一个main方法
    public class Application {
    
    
    //静态的方法和非静态的方法区别很大
    //静态的方法:方法的调用之和左边,定义的数据类型有关
    //非静态的方法:重写
        public static void main(String[] args) {
    
    
    
            A a = new A();
            a.test();//A
    
            //父类的引用指向了A
            
            B b = new A();//子类重写了父类的方法
            b.test();//B
            
        }
    }
    
     
    package com.oop.Demo05;
    
    public class A  extends B{
        //override 重写
       @Override//注解:有功能的注释
        public  void test() {
            // TODO Auto-generated method stub
          //  super.test();
          System.out.println("A=>test()");
        
        }
    
    
    
    }
    
    
    package com.oop.Demo05;
    //重写都是方法的重写,和属性无关
    public  class B {
        public  void test() {
            System.out.println("B=>test()");
        }
    }
    
    

4.3多态

  • 动态编译:类型:可扩展性
  • 即同意方法可以根据发生对象的不同而采用2多种不同的行为方式
  • 一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多
  • 注意事项
    1. 多态是方法的多态,属性没有多态
    2. 父类和子类,有联系 类型转换异常! ClassCastException!
    3. 存在条件:继承关系, 方法需要重写。父类引用指向子类对象!father f1= new Son();
      • 不能重写的方法
        1. static 方法,属于类,它不属于实力
        2. final 常量
        3. private 方法
package com.oop;


import com.oop.Dmeo06.Person;
import com.oop.Dmeo06.Student;

//一个项目应该只存一个main方法
public class Application {


    public static void main(String[] args) {
        //一个对象的实际类型是确定的
        // new Student();
        // new Person();


        //可以指向的引用类型就不确定了:父类的引用指向子类

        //student 能调用的方法都是自己的或者继承父类的!
        Student s1 = new Student();

        //person父类型,可以指向子类,但不能调用子类独有的方法
        Person s2 = new Student();

        Object s3 = new Student();



        //对象能执行哪些方法,主要看对象左边的类型,和右边的关系不大
        s2.run(); 
        
        s1.run();

        
        
    }
}

 
package com.oop.Dmeo06;

public class Person {

    public void run(){
        System.out.println("run");
    }
    
}



package com.oop.Dmeo06;

public class Student extends Person{

    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println("son");
    }

    public void eat(){
        System.out.println("eat");
    }
    
}
  • instanceof(类型转换)引用类型, 判断一个对象是什么类型~
    public static void main(String[] args) {
        

        //obect>>person>>student
        Object object = new Student();
        
        System.out.println(object instanceof Student);

        System.out.println(object instanceof Person);

        System.out.println(object instanceof Object); 
        
        System.out.println(object instanceof Teacher);

        System.out.println(object instanceof String);

        Person person = new Student();

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


        System.out.println(person instanceof Student);

        System.out.println(person instanceof Person);

        System.out.println(person instanceof Object); 
    

    }
  • 类型转换
    1. 父类引用指向子类的对象
    2. 把子类转换为父类,向上转型
    3. 把父类转换为子类,向下转型;强制转换
    4. 方便方法的调用,减少重复的代码!简洁
  public static void main(String[] args) {
    
        //类型之间的转换:父类  子类


        //高                  低
        Person student = new Student();

        //student将这个对象转换为student类型,我们就可以使用student的类型转换方法了

        ((Student)student).go();


        //子类转换为父类,可能丢失自己的本来的一些方法!

        Student student2 = new Student();
        student2 .go();
        Person person = student;
    }

4.4static和代码块以及静态导入包

package com.oop.Demo07;
//static:
public class Student {
    private static  int age;//静态变量 多线程
    private double score;//非静态变量


    public  void run(){}
    public  static  void go(){}
    public static void main(String[] args) {
        Student student = new Student();
        System.out.println( Student.age);//静态
        System.out.println( student.age);
        System.out.println( student.score);
        Student.go();
        student.run();
    }

package com.oop.Demo07;

public class Person {
    {
        //2--赋初始值
        //代码块(匿名代码块)
        System.out.println("匿名代码块");
    }
    static {
        //1--只有一次
        //静态代码块
        System.out.println("静态代码块");
    }
    public  Person(){
        System.out.println("构造方法");
    }

    public static void main(String[] args) {
        Person person1  = new Person();
        //顺序
        //静态代码块(只有一次)
        //名代码块
        System.out.println("============");
        Person person2  = new Person();
    }
}
package com.oop.Demo07;
import  static  java.lang.Math.random;//静态导入包
public class Test {
    public static void main(String[] args) {
        System.out.println(random());
    }
}
  • 常量不能被继承

五、抽象类和接口

5.1抽象类

  • abstract修饰符可以用来修饰方法也可以修饰类,如果修饰方法,那么该方法就是抽象方法,如果修饰类那么该类就是抽象类
  • 抽象类中可以没有抽象方法,但是有抽象方法的类一定要声明为抽象类
  • 抽象类,不能使用new关键字来创建对象,它是用来子类继承的
  • 抽象方法,只有方法的声明,没有方法的实现,它是用来让子类实现的
  • 子类继承抽象类,那么就必须要实现抽象类没有实现的抽象方法,否则该子类也要声明为抽象类
package com.oop.Demo08;
//抽象类abstractv: extends :单继承  接口可以多继承
public  abstract  class Action {
    //约束 有人来帮我实现
    //abstrac 抽象方法,只有方法的名字,没有方法的实现
    public abstract void  doSometing();
    //1.不能new出来,只能靠子类来实现它,约束
    //2.抽象类中可以写普通方法
    //3.抽象方法必须在抽象类中
    //抽象的抽象:约束
}


package com.oop.Demo08;
//抽象类的所有方法,继承了它的子类,都必须要实现它的方法,除非子类也是抽象类
public class A extends Action
{

    @Override
    public void doSometing() {

    }
}

5.2接口

  • 普通类:只有具体实现
  • 抽象类:具体实现和规范(抽象方法)都有!
  • 接口:只有规范
  • 接口就是规范,定义的是一组规则,体现了实现世界中“如果你是……则必须能……”的思想。如果你是天使则必须能飞。如果你是汽车,则必须能跑。如果你好人,则必须干掉坏人;如果你是坏人,则必须欺负好人。
  • 接口的本质是契约,就像我们人间的法律一样。制定好后大家都遵守
  • 00的精髓,是对对象的抽象,最能体现这一点的就是接口,为什么我们讨论设计模板都只针对具备了抽象能力的语言(比如C++,Java,C#等),就是因为设计模板所研究的,实际上就是如何合理的去抽象
  • 作用
    1. 约束
    2. 定义一些方法,让不同的人实现
    3. public abstra
    4. public static final
    5. 接口不能被实例化,接口中没有构造方法
    6. implements可以实现多个接口
    7. 必须重写接口中的方法
package com.oop.Demo09;
//抽象类:extends
//类 可以实现接口implents 接口
public class UserServiceImpl implements  UserService,TimeService {
//实现了接口的类,需要重写接口中的方法
    //多继承  利用接口实现伪多继承
    @Override
    public void sun(String name) {

    }

    @Override
    public void delete(String name) {

    }

    @Override
    public void add(String name) {

    }

    @Override
    public void pudate(String name) {

    }

    @Override
    public void query(String name) {

    }

    @Override
    public void timer() {

    }
}

package com.oop.Demo09;

import java.sql.Struct;

//关键字interface
public interface UserService {
    //常量
    public  static  final int AGE = 99;

    //接口中的所有定义其实都是抽象的public
    public abstract  void sun(String name);
    void delete(String name);
    void add(String name);
    void pudate(String name);
    void query(String name);
}


package com.oop.Demo09;

public interface TimeService {
    void timer();
}

六、内部类及OOP实战

6.1内部类

  • 内部类就是在一个类的内部再定义一个类,比如,A类中定义一个B类,那么B类相对A类来说就是成为内部类,而A类相对B类来说就是外部类了

    1. 成员内部类

      package com.oop.Demo10;
      
      public class Outer {
          private  int id = 10;
          public  void out(){
              System.out.println("这是外部类的方法");
          }
          public class Inner{
              public  void in(){
                  System.out.println("这是内部类的方法");
              }
              //获得外部类的私有ID
              public  void getID(){
                  System.out.println(id);
              }
          }
      }
      
      package com.oop;
      
      
      import com.oop.Demo05.Teacher;
      import com.oop.Demo10.Outer;
      import com.oop.Dmeo06.Person;
      import com.oop.Dmeo06.Student;
      
      //一个项目应该只存一个main方法
      public class Application {
      
      
          public static void main(String[] args) {
             Outer outer =  new Outer();
             Outer.Inner inner= outer.new Inner();
             inner.in();
             inner.getID();
          }
          
      }
      
       
      
      
    2. 静态内部类

    3. 局部内部类

      package com.oop.Demo10;
      
      public class Outer2 {
          //局部内部类
          public void method(){
              class  Inner{
                  public void in(){
      
                  }
              }
          }
      }
      
      
    4. 匿名内部类

      package com.oop.Demo10;
      
      import com.oop.Demo08.A;
      
      public class Test {
          public static void main(String[] args) {
              Apple apple = new Apple();
              //没有名字初始化类,不用将实例保存到变量中
              new Apple().eat();
              UserService userService = new UserService() {
                  @Override
                  public void hello() {
      
                  }
              };
          }
      }
      class Apple{
          public  void eat(){
              System.out.println("1");
          }
      }
      interface UserService{
          void hello();
      }
      
      
posted @ 2021-11-27 17:30  项sir  阅读(70)  评论(0)    收藏  举报
标题