c # Foundation Chapter 3

Posted by tranzparency on Wed, 24 Jul 2019 04:14:03 +0200

What is inheritance? Code examples

    class MyCar: Car
    {
        int people;
 
        public MyCar(int w, float g, int p)
            : base(w, g)
        {
            wheels = w;
            weight = g;
            people = p;
 
        }
 
        public void info()
        {
            Console.WriteLine("I have{0}Twenty wheels", wheels);
            Console.WriteLine("I weigh{0}kg", weight);
            Console.WriteLine("I can take it.{0}personal", people);
        }
        public static void Main(String[] args)
        {
            MyCar myCar = new MyCar(4, 2000, 20);
            myCar.info();
            myCar.speak();
            Console.ReadLine();
        }
    }

What is polymorphism? Code examples

   class Gun
    {
        //Aim
        public void Loaded() {
            Console.WriteLine("Aim before shooting!");
        }
        //Shoot
        public virtual void Fire() {
            Console.WriteLine("Ouch!!");
        }
    }
    class AK47 : Gun
    {
        public new  void Loaded() {
            Console.WriteLine("AK47 Also need to aim?");
        }

        //Rewriting the method of shooting in the parent class
        public override void Fire()
        {
            Console.WriteLine("Ak47,Da Da Da Da Da Da....");
        }
    }

What is an abstract class? Code examples

 public abstract class Person
    {
        public abstract void Show();
        public abstract string Name { get;set;}
        public abstract string this[int n] { get;set;}
       private  int a;
    }
    public class Employee : Person
    {
        private string pname;
        private string[] friends = new string[100];
        public override void Show()
        {
            Console.WriteLine("Full name:{0}", this.pname);
        }
        public override string Name
        {
            get { return this.pname; }
            set { this.pname = value; }
        }
        public override string this[int n]
        {
            get { return this.friends[n]; }
            set { this.friends[n] = value; }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
         //   Person p = new Employee();/* Abstract class object reference subclass instance*/
            Employee p = new Employee();
            p.Name = "Cao Cao";
            p.Show();
            Console.ReadLine();
            }
    }
}

What are the similarities and differences between abstract classes and interfaces?

 

Similarities:

(1) Abstract classes and interfaces can not be instantiated directly. They must be indirectly instantiated by implementing subclasses (common classes) or implementation classes of all abstract methods. If abstract methods are not fully implemented, then this subclass or implementation class must be defined as abstract classes.

(2) There are no abstract methods in interfaces (marked interfaces) and abstract classes, but once methods are defined, the methods in interfaces must be abstract methods, while the methods in abstract classes have no restrictions (either abstract methods or non-abstract methods).

Difference:

(1) Interfaces are variants of abstract classes, and all methods in interfaces are abstract. An abstract class is a class that declares the existence of a method and does not implement it.
(2) Interfaces can be inherited more than abstract classes.
(3) Interface definition methods can not be implemented, while abstract classes can implement some methods.
(4) The basic data type in the interface is static, but the extraction image is not.
(5) The interface can not contain static code blocks and static methods, while abstract classes can contain static methods and static code blocks.

 

What are the differences and similarities between abstract and virtual methods?

 

Difference

1. Different method keywords

The method keyword of virtual method is: virtual.

The method keywords for abstract classes are abstract

2. Does the method of base class have method body/implementation?

Method of virtual method: Declare and implement method.

Method of abstract classes: declaration only, no method body / no implementation.

Common ground

Subclasses / derived classes override the method of the parent class: the keyword override.

 

Key words that define abstract classes and methods?

abstract

 

What are the methods in the chapter on XML in books? Examples of codes

XmlDocument Xml = new XmlDocument();
            Xml.Load("D://bbb.xml");
            XmlNode eng = Xml.DocumentElement;
            XmlNodeList list = eng.ChildNodes; 
            string aaa = eng.InnerText;      
            string bbb = eng.Name;

What are the methods in the chapter of the document in the book? Examples of codes

string path = "D\\abc.txt";
            string context = "D\\ccc.txt";
            FileStream myfs = new FileStream(path,FileMode.Create);
            StreamWriter mysw = new StreamWriter(myfs);
            mysw.Write(context);
            mysw.Close();
            mysw.Close();

 

Topics: xml