zoukankan      html  css  js  c++  java
  • 封装和继承

    构造函数:

    构造函数 - 创建一个类的函数

    每一个类都有构造函数,访问修饰符必须是public的,并且不需要写任何返回值,方法名与类名一致

    自己写的类 - 用户自定义类型
    如果要使用自定义类型产生出来的  对象  必须有一个实例化的过程

    实例化格式:
    类型名  ss = new 构造函数();

    访问修饰符 -  4个

    public - 公共的,任何地方都可以访问,但是需要引用 命名空间

    private - 私有的,类的内部才可以访问

    internal - 内部的,默认的,程序集中可以访问,程序集就是命名空间

    protected - 被保护的,类的内部和类的父类和子类中可以访问

    封装:

    private 数据类型 _名字;   --成员变量

    public 默认一致 名字      --属性

    {  

      get{ return _名字; }  

      set{ _名字 = value; }

    }

    封装可以保护数据的安全性

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 封装
    {
        class Program
        {
            static void Main(string[] args)
            {
                Student ss = new Student();
                ss.Name = "张三";
                ss.sex="99";
                ss.age = "19";
                Console.WriteLine(ss.sex1);
                Console.ReadKey();
            }
        }
    }
    
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 封装
    {
        class Student
        {
            private string _name;
            /// <summary>
            /// 姓名
            /// </summary>
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
            private bool _sex;
            /// <summary>
            /// 性别
            /// </summary>
            public string sex
            {
                set
                {
                    if (value == "男")
                        _sex = true;
                    else if (value == "女")
                        _sex = false;
                    else
                        _sex = true;
                }
            }
            public string sex1
            {
                get
                {
                    if (_sex == true)
                        return "男";
                    else
                        return "女";
                }
            }
            private string _age;
            /// <summary>
            /// 年龄
            /// </summary>
            public string age
            {
                get { return _age; }
                set
                {
                    if (Convert.ToInt32(value) >= 18)
                        _age = value;
                    else
                        _age="未成年";
    
                }
            }
            
    
        }
    }
    

      

    类中不仅可以有成员变量和属性,还可以有成员方法

    方法又分为,普通方法    静态方法

    普通方法使用: 需要实例化对象,用对象点出来

    静态方法使用:  直接用类名点出来,不需要实例化对象,只需在访问修饰符后面加上static

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace aaaaa
    {
        public class jisuan
        {
            /// <summary>
            /// 计算两个数的加法,返回和
            /// </summary>
            /// <param name="a">要计算的第一个数</param>
            /// <param name="b">要计算的第二个数</param>
            /// <returns></returns>
            public int jiafa(int a, int b) //成员方法
            {
                return a + b;
            }
    
            public static int jianfa(int a, int b)
            {
                return a - b;  
            }
        }
    }
    

      

    继承:

    继承语法:  类名 : 父类名(在类名后面直接写冒号与想要继承的父类名)

    子类也叫做xxx的派生类,超类

    父类也叫做基类

    一个父类可以有无数个子类

    一个子类可以有1个父类

    子类并不是可以继承父类中所有的东西,而是可以继承父类中允许子类继承的内容,这个允许条件是依靠访问修饰符来做的权限

  • 相关阅读:
    5月做题计划(数据结构)
    SRM 545 DIV2
    6月做题计划(递归与分治)
    POJ 3121 The SetStack Computer
    struts2初步学习路线
    my97datepicker日历展示出现中文乱码的问题
    tomcat请求数据的编码设置
    STRUT2传递参数中文乱码解决方法
    js mine 类型javascripttext/javascript,application/javascript, and appliation/xjavascript
    eclipse内存设置参数
  • 原文地址:https://www.cnblogs.com/maxin991025-/p/6102936.html
Copyright © 2011-2022 走看看