面向对象以及三大特性封装继承多态
面向对象
- 面向过程思想
- 步骤清晰简单,第一步做啥,第二步做啥..
- 面对过程适合处理一些较为简单的问题
 
- 面向对象思想
- 物以类聚,分类的思维模式,思考问题首选会解决问题需要哪些分类,然后对分类进行单独思考。最后,才对某个分类下的细节进行面向过程的思索。
- 面向对象适合处理复杂的问题,适合处理需要多人协作的问题
 
- 对于描述复杂的适合,为了从宏观上把握,从整体上合理分析,我们需要使用面向对象的思路,来分析整个系统。但是,具体到微观操作,仍然需要面向过程的思路去处理。
面向对象的本质
- 面向对象的本质就是:以类的方式组织代码,以对象的组织(封装)数据
- 抽象
- 三大特性
- 封装
- 继承
- 多态
 注意,个人感觉,封装指针对对象的封装,保护实现代码;继承是指针对类的继承;多态是针对方法,同一个方法子类父类不同的实现方式
 
- 从认识论角度考虑是现有对象后有类。对象是具体的事物。类,是抽象的是对对象的抽象
- 从代码运行角度考虑是先有类后有对象。类是对象的模板
封装
该漏的漏,该藏的藏。
程序设计需求的“高内聚,低耦合",高内聚就是类的内部数据操作细节自己完成,不允许外部干涉;低耦合:仅暴露少量方法给外部使用。
- 封装(数据的隐藏)
- 通常,应禁止直接访问一个对象中数据的实际表示,而应通过操作接口来访问,这称为信息隐藏。
 
- 属性私有,get/set
public class Student
{  //属性私有
    private String name;
    private int age;
    private String sex;
    //提供一些可以操作这个属性的方法
    //提供一些publid的set,get方法
    public Student() {
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
   //加入校验
    public void setAge(int age) {
        if(0<age&&age<120)
        {this.age = age;}
        else{
            System.out.println("输入年龄不合法");
            this.age=-999;
        }
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
}
/**
 * 封装总结
 * 1.提高程序安全性,保护数据
 * 2.隐藏代码实现细节
 * 3.统一接口
 * 4.系统可维护性增加了
 *
 *
 * */
public class Application {
    public static void main(String[] args) {
     Student student =new Student();
     student.setName("小明");
     student.setAge(-1);
     student.setSex("男");
     System.out.println(student.getName());
     System.out.println(student.getAge());
     System.out.println(student.getSex());
    }
}
/*
* 输入年龄不合法
  小明j
  -999
   男
*
*
* */
继承
- 
继承的本质是对某一批类的抽象,从而实现对现实世界更好的建模 
- 
extends的意思是"扩展",子类是父类的扩展 
- 
java中只有单继承没有多继承! 
- 
继承是类和类之间的一种关系,除此之外,类和类之间的关系还有依赖,组合,聚合等。 
- 
继承关系的两个类,一个为子类(派生类),一个为父类(基类)。子类继承父类,使用关键字extends来表示。 
- 
子类和父类之间,从意义上讲应该具有"is a"的关系 
- 
Object 类 
- 
super:注意点 /** * super注意点: * 1.super调用的父类的构造方法,必须在构造方法的第一行 * 2.super 必须只能出现在子类的方法或者构造方法中! * 3.super和this不能同事调用构造方法!因为都必须在第一行不满足 * * vs this: 与this的差别 * 代表对象的不同: * this:本身调用者这个对象 * super:代表父类对象的引用 * 前提 * this: 没有继承也可以使用 * super:只能继承条件下才可使用 *构造方法: * thi(); 本类的构造 * super()父类的构造 * * * *///java中,所有类都默认直接或间接继承Objec public class Person { //public //protected //default //private int money=100000; String name="人类"; public Person() { System.out.println("Person的无参构造器"); } //如果定义了了有参构造,无参构造必须显示定义 public Person(String name) { System.out.println("Person的有参构造器"); } public void say() { System.out.println(this.name+"说了一句话"); } //私有的方法无法被继承 public void print(){ System.out.println(name); }public class Students extends Person { String name="学生"; public Students() { //隐藏代码:调用了父类的无参构造 //super(); //默认的。调用父类的构造器必须放第一行 //super("人类");如果父类未定义无参构造只能调用有参构造 this("小学生");//调用自己的构造器也必须再第一行 System.out.println("Students的无参构造器"); } public Students(String name ) { System.out.println("Students的有参构造器"); } public void test(String name){ System.out.println(name); System.out.println(this.name); System.out.println(super.name); } public void test1(){ print();//Students this.print();//Students super.print();//Person } public void print(){ System.out.println(name); } }
- 
方法重写 /** * 重写: * 需要继承关系,子类重新父类的方法! * 1.方法名必须相同 * 2.参数列表必须相同 * 3.修饰符范围可以扩大但不能缩小 public->protected->Default->private * 4.抛出异常:范围可以缩小但不能扩大 * *重写,子类的方法和父类必须一致,方法体不一致! * * 为什么要重写: * 1.父类的功能子类不一定需要,或者不一定满足。 * */
public class B {
    public static  void test(){
        System.out.println("B--->test()方法");
    }
    public   void test2(){
        System.out.println("B--->test2()方法");
    }
package oop.demo01.demo6;
//方法重写,都是针对方法与属性无关
public class A extends B {
    public  static void test(){
        System.out.println("A--->test()方法");
    }
    @Override//注解
    public void test2() {
       // super.test2();
        System.out.println("A--->test2()方法");
    }
}
ublic class Application {
     public static void main(String[] args) {
         //静态的方法和非静态的方法区别很大
         //静态方法 //方法的调用只和左边引用有关。也就是定义的数据类型有关
        //方法的调用只和左边引用有关。也就是定义的数据类型有关
        A a=new A();
        a.test();
        //父类的引用指向了子类
        B b=new A();
        b.test();
        System.out.println("-------------");
        A c=new A();
        c.test2();
        //父类的引用指向了子类
        B d=new A();
        d.test2();//子类重写了父类的方法,只跟非静态方法的区别
    }
    /**
     *
     *
     * A--->test()方法
     * B--->test()方法
     * -------------
     * A--->test2()方法
     * A--->test2()方法
     *
     * */
}
多态
- 同一方法可以根据发送的对象不同而采用多种不同的行为方式。
- 一个对象的实际类型是确定的,但是可以指向对象的引用类型有很多
- 多态存在的条件
- 有继承关系
- 子类重写父类方法
- 父类引用指向子类对象
 
- 注意:多态是方法的多态,属性是没有多态性
- instanceof :Java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例。instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例。主要用于(类型转换) 引用类型
 作用:多态的作用是实现动态变量,实现动态编译,类型最终状态是程序执行的时候才能决定;通过多态让程序可扩展性更强
/**
 * 多态注意事项:
 * 1.多态是方法的多态,属性没有多态
 * 2.父类和子类,有联系,类型转换异常 ClassCastException
 * 3.存在条件:继承关系,方法需要重新,父类引用执行子类对象! Father f1=new Song();
 *不能被重写的方法
 * 1.static  方法,属于类,不属于实例
 * 2.final  常量
 * 3.private 方法,私有的
 * */
package oop.demo01.demo7;
public class Student extends Person {
    @Override
    public void eat() {
        System.out.println("Student---->eat()");
    }
    public void study() {
        System.out.println("Student---->study()");
    }
}
/////
package oop.demo01.demo7;
public class Person {
    public void run(){
        System.out.println("Persong-->run()");
    }
    public void eat(){
        System.out.println("Persong-->eat()");
    }
}
/////////////////////
package oop.demo01.demo7;
public class Application {
    public static void main(String[] args) {
        //一个对象的实际类型是确定
        //new Strudent();
        //new Persong();
        //可以指向的引用类型是不确定了,父类的引用指向子类
        Student s1=new Student();
        Person  s2=new Student();
        Object  s3=new Student();
        //对象执行哪些方法,注意看对象左边的类型,和右边关系不大
        //子类能调用的方法都是自己的,或者继承父类的
        s1.run();
        s2.run();
        s1.eat();//子类重新了父类方法,所有都将执行子类方法
        s2.eat();//子类重新了父类方法,所有都将执行子类方法
         
        //对象执行哪些方法,注意看对象左边的类型,和右边关系不大
        //子类能调用的方法都是自己的,或者继承父类的
        s1.study();//子类能调用的方法都是自己的,或者继承父类的
       // s2.study();//父类不能调用子类独有的方法
        ((Student) s2).study();//父类不能调用子类独有的方法,只能强制转换,
       //后期程序执行时像这个s2对象不会知道自个执行的哪个实例,或者哪个方法;这就是动态引用变量
        /**
         * instanceof
         * */
        System.out.println("-------------------");
        //Object-->Persong-->Student
        //Object-->Persong-->Teacher
        //Object-->String
        Object  object=new Student();//
        System.out.println("object instanceof  Student-->"+(object instanceof  Student));//true
        System.out.println("object instanceof  Person-->"+(object instanceof  Person));//true
        System.out.println("object instanceof  Teacher-->"+(object instanceof  Teacher));//false
        System.out.println("object instanceof  Object-->"+(object instanceof  Object));//true
        System.out.println("object instanceof  String-->"+(object instanceof  String));//false
        System.out.println("==========================");
        Person  person=new Person();//instance 看的是实例new Person() 是否为类或者类的子类的实例
        System.out.println("person instanceof  Student-->"+(person instanceof  Student));//fase
        System.out.println("person instanceof  Person-->"+(person instanceof  Person));//true
        System.out.println("person instanceof  Teacher-->"+(person instanceof  Teacher));//false
        System.out.println("person instanceof  Object-->"+(person instanceof  Object));//true
        //System.out.println("object instanceof  String-->"+(person instanceof  String));//异常new Person() 和String 不同支线
        System.out.println("==========================");
        Student  student=new Student();//object 是用的new Student() 是student 这条线的 与Thacher无关
        System.out.println("student instanceof  Student-->"+(student instanceof  Student));//true
        System.out.println("student instanceof  Person-->"+(student instanceof  Person));//true
        System.out.println("student instanceof  Object-->"+(student instanceof  Object));//true
       //System.out.println("student instanceof  Teacher-->"+(student instanceof  Teacher));//异常Student 和Teacher不同支线
       //System.out.println("student instanceof  String-->"+(student instanceof  String));//异常Student 和String 不同支线
    }
}
/*Persong-->run()
Persong-->run()
Student---->eat()
Student---->eat()
Student---->study()
Student---->study()
-------------------
object instanceof  Student-->true
object instanceof  Person-->true
object instanceof  Teacher-->false
object instanceof  Object-->true
object instanceof  String-->false
==========================
person instanceof  Student-->false
person instanceof  Person-->true
person instanceof  Teacher-->false
person instanceof  Object-->true
==========================
student instanceof  Student-->true
student instanceof  Person-->true
student instanceof  Object-->true
*/
/**
 * 引用类型转换
 *
 * */
public class Application2 {
    public static void main(String[] args) {
        //类型之间的转换:基本类型 高64 32 16 如果是高转低需要强转
        //子类转为父类,低转高,不需要强制类型转换。可能丢失自己本身的方法
        //高             低
        Person person=new Student();
       //person.study();//需要将person 这个对象转为Student类型,才能使用Student独有的方法
        Student student=((Student) person);
        student.study();
        person=student;//子类转为父类,低转高,不需要强制类型转换。可能丢失自己本身的方法
        /**
         *
         * 1.父类的引用指向子类的对象
         * 2.把子类转化未父类,向上转型
         * 3.把父类转化为子类,向下转化,要强制转化
         * 4.方便方法调用,减少重复代码,提高利用率
         * */
 
                     
                    
                 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号