C# 类的派生 输出个人信息
生活随笔
收集整理的這篇文章主要介紹了
C# 类的派生 输出个人信息
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
運行結果
代碼
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;namespace 派生 {public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){Undergraguate u = new Undergraguate();string name = textBox2.Text;int age = int.Parse(textBox3.Text);string subject = textBox4.Text;textBox1.Text = u.GetMessage(name, age, subject);//直接將返回值輸出到textboxtextBox1.Text += "\r\n" + u.Study();}}public class Student//基類{protected string name;protected int age;public string Study(){string result;result = string.Format("Student({0}):我是基類的方法。我今年{1}歲,我沒畢業,正在學習。\r\n", name, age);return result;}public Student(){this.age = 8;}}public class Undergraguate : Student//派生類 繼承于基類 基類中的函數自動吸收進來{public string subject;//派生類增加的特殊數據成員//public Undergraguate()//構造函數 這樣就不調用基類的構造函數: base("無名",0)這樣調用基類的構造函數//{// subject = "未知";//}public Undergraguate() : base()//繼承的構造函數 大括號里面還能賦值 本程序中這段代碼對輸出結果無作用{subject = "軟工";age++;}public string GetMessage(string name, int age, string subject){this.name = name;this.age = age;this.subject = subject;string result;result = string.Format("Undergraguate({0}):我是派生類的方法。我今年{1}歲,我畢業了,專業是:{2}\r\n", name, age, subject);return result;}} }總結
以上是生活随笔為你收集整理的C# 类的派生 输出个人信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【广义找零钱问题】 贪心算法求解进制转换
- 下一篇: C# 静态方法和属性 图书管理