zoukankan      html  css  js  c++  java
  • 0103-面向对象类的特性

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _02面向对象
    {
        class Program
        {
            static void Main(string[] args)
            {
                Person p=new Student();
                p.Age = -20;
                Console.WriteLine(p.Age); //年龄-20读取是会被属性保护输出为18
                p.SayHello();  //输出:我是人类
                Student s=((Student)p);
                s.SayHello();//输出:我是学生
                s.SecName = "女超人";
                s.Test();
                //创建教师对象实例
                Teacher th=new Teacher("阿程",28,'',89,96,98);
                th.SayHi();//我叫阿程,我是男生,今年28岁
                th.ShowScore(); //我叫阿程,总成绩283,平均分94
                Teacher t = new Teacher("春老师", 18, '');
                t.SayHi();
                Console.ReadKey(); 
            }
        }
        /*
         //面向对象的三大特性:封装、继承、多态
         new 创建对象:1、在堆中开辟内存空间 2、在堆中创建对象 3、调用对象的构造函数
         this 1、代表当前类的对象 2、显式的调用自己的构造函数
         base 1、调用父类的成员,包括调用父类的构造函数
    
         字段:存储数据
         属性:保护字段
         函数:描述对象的行为
         构造函数:初始化对象,给对象的属性进行赋值
         */
        class Person
        {
            private string _name;
            private int _age;
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
    
            public int Age
            {
                get //属性值保护
                {
                    if (_age <= 0 || _age > 160)
                    {
                        return _age = 18;
                    }
                    return _age;
                }
                set { _age = value; }
            }
    
            public char Gender { get; set; }
    
            public void SayHello()
            {
                Console.WriteLine("我是人类");
            }
        }
        class Student:Person
        {
            public string SecName { get; set; }
            public new void  SayHello() //使用new关键字可以隐藏父类方法
            {
                Console.WriteLine("我是学生");
            }
    
            public void Test()
            {
                string SecName;
                SecName = "弹力女超人";
                Console.WriteLine("{0}",SecName); //局部变量
                Console.WriteLine("{0}",this.SecName); //成员变量
            }       
        }
    
        class Teacher:Person
        {
            public int Chinese { get; set; }
            public int Math { get; set; }
            public int English { get; set; }
            public Teacher(string name,int age,char gender,int chinese,int math,int english)
            {
                this.Name = name;
                this.Age = age;
                this.Gender = gender;
                this.Chinese = chinese;
                this.English = english;
                this.Math = math;
            }
            //以下两个构造方法通过this关键字调用本身的全参的构造方法
           public Teacher(string name,int age,char gender):this(name,age,gender,0,0,0)
            {
            }
    
            public Teacher(string name, int chinese, int math, int english) : this(name, 0, '', chinese, math, english)
            {
            }
    
            public void SayHi()
            {
                Console.WriteLine("我叫{0},我是{1}生,今年{2}岁",this.Name,this.Gender,this.Age);
            }
    
            public void ShowScore()
            {
                Console.WriteLine("我叫{0},总成绩{1},平均分{2}",this.Name,
                    this.Chinese+this.Math+this.English,(this.Chinese + this.Math + this.English)/3);
            }
        }
    }
    凡哥,别他妈吹牛逼了
  • 相关阅读:
    python目录
    Django的认证系统
    Django logging配置
    Django 中间件
    Django Form和ModelForm组件
    AJAX
    Django中ORM介绍和字段及字段参数
    Django ORM那些相关操作
    Python中应该使用%还是format来格式化字符串?
    利用Python win32api 模拟键盘输入,打开浏览器,实现网页全屏,并移到第二屏幕
  • 原文地址:https://www.cnblogs.com/sdlz/p/14690328.html
Copyright © 2011-2022 走看看