zoukankan      html  css  js  c++  java
  • Java面向对象二

    package com.oop;
    
    import com.oop.demo03.Pet;
    
    public class Application {
        public static void main(String[] args) {
            Pet dog = new Pet();
            dog.name = "旺财";
            dog.age = 3;
            dog.shout();
    
            System.out.println(dog.name);
            System.out.println(dog.age);
    
            Pet cat = new Pet();
        }
    }
    
    package com.oop.demo03;
    
    public class Pet {
        public String name; //public 修饰符方便调用
        public int age;
    
        //无参构造
    
        public void shout() {
            System.out.println("叫一声");
        }
    }
    
    package com.oop;
    
    public class Application {
        public static void main(String[] args) {
            /*
            1. 类与对象
                类是一个模板:抽象,对象是一个具体的实例
            2. 方法
                定义、调用!
    
            3. 对应的引用
                引用类型:   基本类型(8)
                对象是通过引用来操作的:栈---》堆
    
            4. 属性:字段 Field 成员变量
                默认初始化:
                    数字: 0   0.0
                    char: 'u0000'
                    boolean: false
                    引用: null
    
                修饰符  属性类型  属性名  =  属性值!
    
            5. 对象的创建和使用
                - 必须使用new 关键字创建对象,构造器  Person person = new Person();
                - 对象的属性  person.name
                - 对象的方法  person.sleep()
    
            6. 类
                静态的属性   属性
                动态的行为   方法
    
            '封装、继承、多态'
    
             */
    
        }
    }
    

    封装

    package com.oop;
    
    import com.oop.demo04.Student;
    
    /*
        1. 提高程序的安全性,保护数据
        2. 隐藏代码的实现细节
        3. 统一端口
        4. 系统可维护增加了
     */
    
    public class Application {
        public static void main(String[] args) {
    
            Student student = new Student();
            student.setName("tom");
            System.out.println(student.getName());
    
            student.setAge(999);//不合法
            System.out.println(student.getAge());
    
        }
    }
    
    package com.oop.demo04;
    
    // 类    private:私有
    
    public class Student {
    
        //属性私有
        private String name;//名字
        private int id;//学号
        private char sex;//性别
        private int age;
    
        //提供一些可以操作这个属性的方法!
        //提供一些public 的 get、set方法
    
        //get 获得这个数据
        public String getName(){
            return this.name;
        }
    
        //set 给这个数据设置值
        public void setName(String name){
            this.name = name;
        }
    
        //Ait+insert    select Getter and Setter
        public char getSex() {
            return sex;
        }
    
        public void setSex(char sex) {
            this.sex = sex;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            if (age>=0 && age<=120){
                this.age = age;
            }else{
                this.age = 3;
            }
    
        }
    }
    

    继承

    package com.oop;
    
    import com.oop.demo05.Student;
    
    public class Application {
        public static void main(String[] args) {
    
            Student student = new Student();
            student.say();
            System.out.println(student.getMoney());
        }
    }
    
    package com.oop.demo05;
    
    //在Java中,所有的类,都默认直接或间接继承Object
    //Person   人 : 父类
    public class Person /*extends Object*/{
    
        //public   公开
        //protected    受保护的   用该修饰符声明的变量,其他类是无法调用的
        //default
        //private	私有的  类似protected
        private int money = 10_0000_0000;
    
        public void say(){
            System.out.println("Hello World!");
        }
    
        public int getMoney() {
            return money;
        }
    
        public void setMoney(int money) {
            this.money = money;
        }
    
    }
    
    package com.oop.demo05;
    
    //学生   is  人 :派生类,子类
    //子类继承了父类,就会拥有父类的全部方法!
    public class Student extends Person {
    
        // Ctrl+H   查看继承关系
    }
    



    package com.oop;
    
    import com.oop.demo05.Student;
    
    public class Application {
        public static void main(String[] args) {
            Student student = new Student();
            student.test("alice");
    
            student.test1();
    
        }
    }
    
    输出结果:
    Person无参执行
    Student无参执行
    alice
    jerry
    tom
    Student
    Student
    Person
    
    package com.oop.demo05;
    
    public class Person {
    
        public Person() {
            System.out.println("Person无参执行");
        }
    
        protected String name = "tom";
    
        //private 私有的东西无法被继承
        public void print(){
            System.out.println("Person");
        }
    
    }
    
    package com.oop.demo05;
    
    public class Student extends Person {
    
        public Student() {
            //隐藏代码:调用了父类的无参构造
            super(); //调用父类的默认构造器,必须要在子类默认构造器的第一行
            System.out.println("Student无参执行");
        }
    
        private String name = "jerry";
        public void test(String name){
            System.out.println(name);//alice
            System.out.println(this.name);//jerry
            System.out.println(super.name);//tom
        }
    
        public void print(){
            System.out.println("Student");
        }
    
        public void test1(){
            print();//Student
            this.print();//Student
            super.print();//Person
        }
    }
    
    super注意点:
        1. super调用父类的构造器方法,必须在构造器方法的第一个
        2. super必须只能出现在子类的方法或构造器中!
        3. super和this不能同时调用构造方法!
    
    Vs this:
        代表的对象不同:
            this:本身调用者这个对象,调用当前类的对象(属性、方法)
            super:代表父类对象的应用,调用父类的对象(属性、方法)
        前提:
            this:没有继承也可以使用
            super:只能在继承条件才能使用
        构造方法:
            this(); 本类的默认构造
            super(); 父类的默认构造
    



    package com.oop;
    
    import com.oop.demo05.A;
    import com.oop.demo05.B;
    
    public class Application {
        public static void main(String[] args) {
    
            //方法的调用只和左边,定义的数据类型有关
            A a = new A();
            a.test();//A=>test
    
            //父类的引用指向了子类
            B b = new A();
            b.test();//B=>test
    
        }
    }
    /*
    static 修饰符进行声明,是与类一起进行加载的
    加载出的结果在进行调用、重写时是不会再去重新加载的
    */
    
    package com.oop.demo05;
    
    public class A extends B {
        public static void test(){
            System.out.println("A=>test");
        }
    }
    
    package com.oop.demo05;
    
    //重写都是方法的重写,和属性无关
    public class B {
        public static void test(){
            System.out.println("B=>test");
        }
    }
    



    package com.oop;
    
    import com.oop.demo05.A;
    import com.oop.demo05.B;
    
    public class Application {
        public static void main(String[] args) {
        //静态的方法和非静态的方法区别很大!
            //静态方法: 方法的调用只和左边,定义的数据类型有关;和类一起进行加载,无法使用重写
        //非静态:可以使用重写
            A a = new A();
            a.test();//A=>test
    
            //父类的引用指向了子类
            B b = new A();//子类重写了父类的方法
            b.test();//A=>test
    
    //        A a1 = new B();   //提示需要的是A,提供的是B
    //        a1.test();
    
    //        b.nameA();//Cannot resolve method 'nameA' in 'B'   无法调用
            b.nameB();//BBB
    
            System.out.println(b.AA);//BB
    
            B b1 = new B();
            b1.test();//B=>test
    
    
        }
    }
    /*
    重写个人理解:重新写入
    1.
    A继承B的属性与方法,当执行A的test方法时,由于是继承关系,A的test方法就是B的test方法
    而A使用了重写功能,将原本应继承B的test方法,改为使用A所创建的test方法
    以此证明第一次调用 A a = new A(); 为什么输出的是A所设定的值
    
    2.
    B b = new A();
    方法的调用只和左边,定义的数据类型有关   --- 这句不能理解
    B 是数据类型,而起决定因素的是 new A(),new谁就是要调用谁
    之所以调用A方法时可以将数据类型写为B,是因为继承关系的原因
    以此证明第第二次调用 B b = new A(); 为什么输出的是A所设定的值
    
    3.
    调用A时数据类型写为B可以执行,因为A继承B
    调用B时数据类型不得写为A,B没有继承A这一层关系
    
    4.
    经过 ”b.nameA();“、”b.nameB();“ 和 ”System.out.printf(b.AA);“ 的验证
     B b = new A();
     b 不是等于A类,因为 b.nameA() 调用时提示没有该方法,而 b.nameB() 时是可以进行调用的,结果为 ’BBB'
     并且进一步验证 System.out.printf(b.AA) ,输出的是 ‘BB'
     由此看出 2. 说的是不对的
     方法的调用只和左边,定义的数据类型有关    --- 这句也说明调用谁看数据类型
     ”B b = new A();“ 调用的是B类
     ”b.test();“ 输出的结果是 ‘A=>test’,因为 A(子类)进行重写操作,并且调用格式写为 ”B b = new A();“
     会影响到 B(父类)所设定的对象
    
     5.
     ”B b1 = new B();“ 直接调用 B 类,没有 A 类的事情,所以 ”b1.test();“ 输出的值时 ‘B=>test’
     */
    
    package com.oop.demo05;
    
    //继承
    public class A extends B {
    
        //Override 重写
        @Override  //注解:有功能的注释!
        public void test() {
            System.out.println("A=>test");
        }
        public void nameA(){
            System.out.println("AAA");
        }
        public String AA = "AA";
    }
    
    package com.oop.demo05;
    
    //重写都是方法的重写,和属性无关
    public class B {
        public void test(){
            System.out.println("B=>test");
        }
        public void nameB(){
            System.out.println("BBB");
        }
    
        public String AA = "BB";
    }
    
    重写:需要有继承关系,子类重写父类的方法!
        1. 方法名必须相同
        2. 参数列表列表必须相同
        3. 修饰符:范围可以扩大但不能缩小: public > protected > default > private
        4. 抛出异常:范围,可以被缩小,但不能扩大;ClassNotFoundException ---> Exception(大)
    
    重写,子类的方法和父类必须一致;方法体不同!
    
    为什么需要重写:
        1. 父类的功能,子类不一定需要,或者不一定满足!
        Alt + Insert : override;
    
  • 相关阅读:
    计数
    表单验证
    获取位置
    jq事件
    jq选择器
    PHP超文本预处理器(通用开源脚本语言)
    文字超出两行显示省略号
    Vim配置:在win10下用vim编译运行C/C++(异步插件管理,一键运行)
    实验楼HTML基础入门学习
    博客园美化:添加目录,标题设置,代码高亮,主题设置
  • 原文地址:https://www.cnblogs.com/Notesdata/p/14153052.html
Copyright © 2011-2022 走看看