zoukankan      html  css  js  c++  java
  • 12.访问修饰符的不同作用域

    package test.po;
    
    //学生类
    public class Student {
        //属性
        private String name;//姓名
        //分别设置age属性为不同访问修饰符修饰
        /*public int age; 
        int age;
        protected int age;*/
        private int age;//年龄
        private double weight;//体重
        
        //无参构造方法
        public Student() {
            //使用无参构造方法构造学员时,为其属性赋值
            this.name="无名氏";
            this.age=18;
            this.weight=50;
        }
    
        //构造方法重载 :带参构造方法
        public Student(String name, int age, double weight) {
            this.name = name;
            this.weight = weight;
            if(age<18 || age>30){
                System.out.println("***输入的年龄为:"+age+",该年龄不合法,将重置!***");
                this.age=18;
            }else{
                this.age = age;
            }
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            if(age<18 || age>30){
                System.out.println("***输入的年龄为:"+age+",该年龄不合法,将重置!***");
                return;
    
            }
            this.age = age;
        }
    
        public double getWeight() {
            return weight;
        }
    
        public void setWeight(double weight) {
            this.weight = weight;
        }
    
        //方法
        //分别设置study()为不同访问修饰符修饰
        //private void study(){
        //void study(){
        //protected void study(){
        public void study(){
            System.out.println(name+"说:好好学习,天天向上!");
        }
    }
    package test.oop.demo;
    
    import test.po.Student;
    
    public class TestStudent {
        //测试不同访问修饰符的不同作用域
        public static void main(String[] args) {
            Student student=new Student();
            //测试不同访问修饰符的方法或属性的作用域
            /*student.study(); 
            student.age;*/
        }
    }
  • 相关阅读:
    bzoj 3196/tyvj p1730 二逼平衡树
    AW201 可见的点 (欧拉函数)
    P3912 素数个数
    P1029 最大公约数和最小公倍数问题
    P1835 素数密度
    P2563 [AHOI2001]质数和分解
    P1075 质因数分解
    AW199 余数之和
    AW198 反素数
    AW197 阶乘分解
  • 原文地址:https://www.cnblogs.com/xiaotaoxu/p/5536454.html
Copyright © 2011-2022 走看看