• 面向对象的复习_封装


    封装
    字段:存储数据,访问修饰符应该设置为private私有的

    string _name;

    属性:保护字段,对字段的取值和赋值进行限定

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    构造函数:初始化对象,当创建对象的时候会调用构造函数

    public Person(string name)
    {
        this.Name = name;           
    }

    new关键字:

    Person p = new Person();

    1.在堆中开辟空间

    2.在开辟的空间中创建对象

    3.调用对象的构造函数


    this关键字:

    1.代表当前类的对象

    2.调用自己的构造函数

     1     class Person
     2     {
     3         //字段 属性 构造函数 方法 接口
     4         string _name;//字段
     5         public string Name//属性
     6         {
     7             get { return _name; }
     8             set//限定_name的值
     9             {
    10                 if (value != "张三")
    11                 {
    12                     value = "张三";
    13                 }
    14                 _name = value;
    15             }
    16         }
    17         int _age;
    18         public int Age
    19         {
    20             get//限定_age的取值范围
    21             {
    22                 if (_age < 0 || _age > 100)
    23                 {
    24                     return _age = 0;
    25                 }
    26                 return _age;
    27             }
    28             set { _age = value; }
    29         }
    30         char _gender;
    31         public char Gender
    32         {
    33             get { return _gender; }
    34             set { _gender = value; }
    35         }
    36         int _chinese;
    37         public int Chinese
    38         {
    39             get { return _chinese; }
    40             set { _chinese = value; }
    41         }
    42         int _math;
    43         public int Math
    44         {
    45             get { return _math; }
    46             set { _math = value; }
    47         }
    48         int _english;
    49         public int English
    50         {
    51             get { return _english; }
    52             set { _english = value; }
    53         }
    54         //构造函数
    55         public Person(string name, int age, char gender, int chinese, int math, int english)
    56         {
    57             this.Name = name;
    58             this.Age = age;
    59             if (gender != '' && gender != '')//对字段的取值进行限定
    60             {
    61                 gender = '';
    62             }
    63             this.Gender = gender;
    64             this.Chinese = chinese;
    65             this.Math = math;
    66             this.English = english;
    67         }
    68 
    69         //通过this调用自身的构造函数
    70         public Person(string name, int age, char gender)
    71             : this(name, age, gender, 0, 0, 0)
    72         {
    73             this.Name = name;
    74             this.Age = age;
    75             this.Gender = gender;
    76         }
    77 
    78 
    79         public void SayHello()
    80         {
    81             Console.WriteLine("{0}------{1}-----{2}", this.Name, this.Age, this.Gender);
    82         }
    83     }
    View Code
    
    
  • 相关阅读:
    Java调度实现
    关于《报表》的实际运用案例
    mybaits错误解决:There is no getter for property named 'parentId ' in class 'java.lang.String'
    Java Eclipse进行断点调试
    切割时间工具类
    JavaWeb开发技术基础概念回顾篇
    解决无线网络连接出现黄色感叹号---win10
    登录界面Demo
    MD5加密Demo
    java.lang.NullPointerException&com.cb.action.LoginAction.execute(LoginAction.java:48)
  • 原文地址:https://www.cnblogs.com/amixc/p/4019983.html
Copyright © 2020-2023  润新知