c#12th Day of the Road to Self-Study
1. Notes
1. Inheritance (e.g., inheritance)
Characteristic:
1. Uniformity: A word class can have only one parent
2. Transitivity:
We may write duplicate members in some classes, and we can encapsulate these duplicate members in a single class as the parent of those classes.
Word classes inherit the methods and properties of the parent class, do not inherit private character segments of the parent class, nor do they inherit constructors of the parent class, but by default, word classes invoke constructors of the parent class that have no parameters to create parent objects so that children can use members of the parent class.
Therefore, if a parameterized constructor is rewritten in the parent class, the nonparameterized constructor is eliminated and the word class cannot be called, so the word class will error.
Solution:
1) Rewrite a parameterless constructor in the parent class
2) Call the parent class constructor as shown in the word class, using the keyword base() (commonly used)
2. View Class Diagram
Corresponding Project - > View
3.object is the base class for all classes
4.new keywords
1) Create Objects
2) Hiding a member of the same name that inherits from the parent hides the consequence of not calling a member of the parent
2. Code
namespace inherit { class Program { static void Main(string[] args) { Student s = new Student("Student", 18, 'male', 101); } } public class Person { private string _name; public string Name { get { return _name; } set { _name = value; } } private int _age; public int Age { get { return _age; } set { _age = value; } } private char _gender; public char Gender { get { return _gender; } set { _gender = value; } } private void CHLSS() { Console.WriteLine("Eat, drink, and sleep in Laza"); } public Person(string name, int age, char gender) { this.Name = name; this.Age = age; this.Gender = gender; } } public class Student : Person { public Student(string name, int age, char gender, int id) : base(name, age, gender) { //this.Name = name; //this.Age = age; //this.Gender = gender; this.Id = id; } private int _id; public int Id { get { return _id; } set { _id = value; } } public void Study() { Console.WriteLine("I will learn"); } } public class Teacher : Person { public Teacher(string name, int age, char gender, double salary) : base(name, age, gender) { this.Salary = salary; } private double _salary; public double Salary { get { return _salary; } set { _salary = value; } } public void Teach() { Console.WriteLine("I can teach"); } } public class Driver : Person { public Driver(string name, int age, char gender, int dirvertime) : base(name, age, gender) { this.DirverTime = dirvertime; } private int _dirveTime; public int DirverTime { get { return _dirveTime; } set { _dirveTime = value; } } public void Drive() { Console.WriteLine("Drivers drive"); } } }
namespace Inheritance Practice { class Program { static void Main(string[] args) { //Reporter: I am a journalist I Love Videos My age is 34 I am a male puppy //Driver: My name is Schumacher My age is 43 I am a man My driving age is 21 years //Programmer: My name is Sun Quan. My age is 23. I am a boy. My working life is 3 years. Reporter rep = new Reporter("Dogs", 34, 'male', "Take photoes in secret"); rep.ReporterSayHello(); Programmer pro = new Programmer("Programmer", 23, 'male', 3); pro.ProgrammerSayHello(); pro.SayHello(); Console.ReadKey(); } public class Person { private string _name; private int _age; private char _gender; public string Name { get { return _name; } set { _name = value; } } public int Age { get { return _age; } set { _age = value; } } public char Gender { get { return _gender; } set { _gender = value; } } public Person(string name,int age,char gender) { this.Age = age; this.Name = name; this.Gender = gender; } public void SayHello() { Console.WriteLine("Hello, I am a person"); } } public class Reporter:Person { private string _hobby; public string Hobby { get { return _hobby; } set { _hobby = value; } } public void ReporterSayHello() { Console.WriteLine("My name is{0},I'm a puppy and my hobbies are{1},I am{2}Sheng, I'm this year{3}Age", this.Name, this.Hobby, this.Gender, this.Age); } public Reporter(string name,int age,char gender,string hobby) :base(name,age,gender) { this.Hobby = hobby; } public new void SayHello() { Console.WriteLine("Hello, I am a journalist"); } } public class Programmer:Person { private int _workYear; public int WorkYear { get { return _workYear; } set { _workYear = value; } } public void ProgrammerSayHello() { Console.WriteLine("My name is{0},I am a programmer, I am{1}Sheng, I'm this year{2}Age,My working life is{3}year",this.Name,this.Gender,this.Age,this.WorkYear); } public Programmer(string name,int age,char gender,int workYear) :base(name,age,gender) { this.WorkYear = workYear; } public new void SayHello() { Console.WriteLine("Hello, I am a programmer"); } } } }
namespace Method Exercise1 { class Program { static void Main(string[] args) { //Example: Write a method to determine if the input year is a leap year bool b=IsRun(2000); Console.WriteLine(b); Console.ReadKey(); } /// <summary> ///Determine if the modified year is a leap year /// </summary> /// <param name="year">year</param> /// <returns>Result of judgment </returns> public static bool IsRun(int year) { bool b = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); return b; } } }
namespace Method Exercise2 { class Program { public static void Main(string[] args) { //Comparing the sizes of two numbers returns the largest int n1 = 10; int n2 = 20; int max=GetMax(n1, n2);//Arguments Console.WriteLine(max); Console.ReadKey(); } /// <summary> ///Calculate the calculated value between two numbers /// </summary> /// <param name="n1">first number</param> /// <param name="n2">second number</param> /// <returns>Maximum number returned </returns> public static int GetMax(int n1,int n2)//Formal parameters { int max = n1 > n2 ? n1 : n2; return max; } } }
namespace Method Exercise3 { class Program { static void Main(string[] args) { //Read Input Integer //Multiple calls (returns if the user enters a number, otherwise prompts the user to re-enter) while(true) { Console.WriteLine("Please enter a number:"); string input = Console.ReadLine(); int number=NewMethod(input); Console.WriteLine(number); Console.ReadKey(); } } /// <summary> ///Determine if the input is a number ///Return if it is a number ///If not, prompt the user to re-enter /// </summary> /// <param name="input">number entered </param> public static int NewMethod(string input) { while(true) { try { int number = Convert.ToInt32(input); return number; } catch { Console.WriteLine("Please re-enter:"); input = Console.ReadLine(); } } } } }
namespace Method Exercise5 { class Program { static void Main(string[] args) { //Calculates the integer sum of an array int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; Console.WriteLine("Arrays and{0}", Sum(nums)); Console.ReadKey(); } /// <summary> ///Array and /// </summary> /// <param name="nums">array</param> /// <returns>and </returns> public static int Sum(int[] nums) { int sum=0; for(int i=0;i<nums.Length;i++) { sum += nums[i]; } return sum; } } }
namespace Method Exercise6 { class Program { static void Main(string[] args) { //There is a string with the longest output string[] name = { "Malone", "Michael Jordan", "Regu Miller", "Tim Duncan", "Kobe Bryant" }; Console.WriteLine(GetLongest(name)); Console.ReadKey(); } public static string GetLongest(string[] s) { string max = s[0]; for (int i = 0; i < s.Length; i++) { if (s[i].Length > max.Length) max = s[i]; } return max; } } }
namespace Method Exercise8 { class Program { static void Main(string[] args) { //Write a method to determine if the number entered by the user is a prime number //Write another method that requires the user to enter only digits if there is a mistake Console.WriteLine("Please enter a number"); string number = Console.ReadLine(); int num = GetNumber(number); Console.WriteLine(num); bool b = IsZhi(num); Console.WriteLine(b); Console.ReadKey(); } /// <summary> ///Enter a number /// </summary> /// <param name="s">Number entered </param> /// <returns>returned integer </returns> public static int GetNumber(string s) { int n; while(true) { try { n = Convert.ToInt32(s); return n; } catch { Console.WriteLine("Please re-enter"); s = Console.ReadLine(); } } } /// <summary> ///Determine if it is a prime number /// </summary> /// <param name="n">Number to judge </param> /// <returns>Judgement </returns> public static bool IsZhi(int n) { if(n<2) { return false; } else { for(int i=2;i<n;i++) { if(n%i==0) { return false; } } return true; } } } }
namespace Method Exercise9 { class Program { static void Main(string[] args) { //After accepting input, determine its level and display it int s; Console.WriteLine("Please enter your test results"); string m = Console.ReadLine(); s = Convert.ToInt32(m); Console.WriteLine(GetLevel(s)); Console.ReadKey(); } public static string GetLevel(int score) { string level = ""; switch (score / 10) { case 10: case 9: level = "excellent"; break; case 8: level = "good"; break; case 7: level = "in"; break; case 6: level = "difference"; break; default: level = "Fail"; break; } return level; } } }