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;*/
        }
    }
  • 相关阅读:
    .Net反编译软件
    Windows下Node.js安装及环境配置
    Servlet处理日期
    Servlet的文件上传
    Eclipse错误:Syntax error on tokens, delete these tokens问题解决
    Servlet中操作数据库
    Servlet的会话(Session)跟踪
    Servlet的Cookies处理
    Servlet的异常处理
    Servlet的过滤器(Filter)
  • 原文地址:https://www.cnblogs.com/xiaotaoxu/p/5536454.html
Copyright © 2011-2022 走看看