封装:可以想象成遥控器,你不需要知道它内部怎么实现的,它封装好你拿来就能用就行了
继承:把这几个类重复的成员单独拿出来封装成一个类,作为它们几个共同的父类
继承的特性:
单根性:只能有一个父类(基类)
传递性:孙子可以用父亲的、爷爷的、祖爷爷的属性(查看类图看它们之间的关系,视图 - 查看类图)
父类Person
using System; using System.Collections.Generic; using System.Text; namespace 继承 { public class Person { private string _name; private int _age; private char _gender; public string Name { get => _name; set => _name = value; } public int Age { get => _age; set => _age = value; } public char Gender { get => _gender; set => _gender = value; } public void CHLSS() { Console.WriteLine("吃喝拉撒睡"); } } }
子类Student
using System; using System.Collections.Generic; using System.Text; namespace 继承 { public class Student:Person { private int id; public int Id { get => id; set => id = value; } public void Study() { Console.WriteLine("学生会学习"); } } }
在Main方法里执行
using System; namespace 继承 { class Program { static void Main(string[] args) { Student s = new Student(); //这4个继承了父类Person的公共部分 s.Name = "春哥"; s.Age = 18; s.Gender = '男'; s.CHLSS(); //这2个是继承了自己那部分 s.Id = 9; s.Study(); } } }
标签:set,继承,System,int,using,public From: https://www.cnblogs.com/xiaochunblog/p/16907352.html