• Java三大特性


    面向对象的三大特性

    1.封装性(属性私有,get,set)

    提高了程序的安全性,保护了数据
    隐藏了代码的实现细节
    统一了接口
    系统的可维护性增加了

    set get方法可以设置安全性检测,来设置正确的数据

    public class Encapusulatin {
        private int id;
        private String name;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }
    public class Application {//通过set,get方法来设置和访问类
        public static void main(String[] args) {
            Encapusulatin en = new Encapusulatin();
            en.setName("封装");
            en.setId(1);
            System.out.println(en.getName()+en.getId());
        }
    }
    

    2.继承性

    1. java中所有类都继承(间接)Object 类。
    2. java只有单继承。
    3. 快捷键打开继承树:ctrl + H
    4. private 不能继承
    5. 权限一共四个等级

    super和this

    • super()必须放在构造器中的第一行,是第一行的代码!默认调用父类的无参构造器。super();方法中可以传参调用父类的含参构造器
    • super只能出现在子类方法或者构造中
    • super()和this()不能同时使用
    • 子类构造方法调用了父类的构造方法

    this 和super的区别

    代表的对象不同

    • this代表本身调用者这个对象
    • super代表父类的引用

    前提不同

    • this 没有继承也能使用
    • super只有继承才能使用

    构造方法

    • this使用本类的构造方法
    • super使用父类的构造方法

    3.多态性

    重写override(目的:父类中的方法无法满足子类的需求了)

    静态方法重写也没用,永远只调用左边的

    Person person = new Student();
    
    • 重写的方法只能用public修饰。
    • 方法名必须相同
    • 必须有继承关系
    • 参数列表一定相同
    • 修饰符的范围可以变大,不可以变小
    • 异常的范围只能变小,不能变大
    public class ThisTest {
        String name;
        public ThisTest() {//使用this调用同类的其他构造方法
            this("LiBai");
            System.out.println(name);
        }
        public ThisTest(String name) {
            this.name = name;
            System.out.println(name);
        }
    }
    public class SuperTest extends ThisTest {
        String name;
        public SuperTest() {
            super();//不传参调用父类
            System.out.println(name);
        }
        public SuperTest(String name) {
            super(name);//传参调用父类的构造方法
            System.out.println(name);
        }
    }
    public class Application1 {
        public static void main(String[] args) {
            ThisTest t = new SuperTest();
        }
    }
    结果:
        LiBai
        LiBai
        null
    
  • 相关阅读:
    asp.net 汉字转拼音类
    NET分页实现及代码
    Web.config配置文件详解(新手必看) (转载)
    偶开通博客啦
    转帖不会乱码的,powershell网络蜘蛛
    ConvertFrom-String 命令研究
    powershell玩转xml之20问
    powershell 判断操作系统版本 命令
    powershell加win的dns服务器,解决网站负载均衡问题
    PowerShell并发控制-命令行参数之四问
  • 原文地址:https://www.cnblogs.com/li33/p/12709926.html
Copyright © 2020-2023  润新知