zoukankan      html  css  js  c++  java
  • 面向对象08封装详解

    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;
    }

    //Alt + insert


    public int getId() {
    return id;
    }

    public void setId(int id) {
    this.id = id;
    }

    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>120 || age<0){//不合法
    this.age = 3;
    }else {
    this.age = age;
    }

    }
    }



    /*
    package com.oop;

    import com.oop.demo03.Pet;
    import com.oop.demo04.Student;
    /*
    1.提高程序的安全型,保护数据
    2.隐藏代码的实现细节
    3.统一接口
    4.系统可维护增加了
    */

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

    Student s1 = new Student();

    s1.setName("leo");

    System.out.println(s1.getName());

    s1.setAge(999);//不合法

    System.out.println(s1.getAge());


    }
    }

    */
  • 相关阅读:
    傻帽
    csc编译c#文件
    真空
    继承,多态及抽象性
    HASH算法
    正则表达式
    js向数组和map添加元素
    详解TypeScript项目中的tsconfig.json配置
    TS:元素隐式具有 “any“ 类型,因为类型为 “any“ 的表达式不能用于索引类型
    yarn基本命令
  • 原文地址:https://www.cnblogs.com/yuanzhihui/p/14873599.html
Copyright © 2011-2022 走看看