Member Variables and Access Modifiers - Exercises

using System;

 

namespace lesson2_ member variables and access modifier exercises

{

    #region Exercise 1

    //What is 3P?

 

    // access modifier

    // public--public self (internal) and others (external) can access and use

    // private--private self (internal) can only be accessed and used without writing, the default is private

    // protected--Protected self (internal) and subclasses can only be accessed and used

 

    #endregion

 

    #region Exercise 2

    //Define a human being with characteristics such as name, height, age, home address, etc.

    class Person

    {

        public string name;

        public float height;

        public int age;

        public string homeAddress;

    }

 

    #endregion

 

    #region Exercise 3

    //Define a student class, which has characteristics such as name, student number, age, deskmate, etc., and has learning methods. Create several students with the student class

    class Student

    {

        public string name;

        public int number;

        public int age;

        public Student deskmate;

    }

    #endregion

 

    #region Exercise 4

    //Define a class class, with professional name, teacher capacity, students, etc. create a class object

    class Class 

    {

        public string name;

        public int capacity;

        public Student[]students;

 

    }

    #endregion

 

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("Member variables and access modifier exercises");

 

            Objects created by #region

            /*Person p = new Person();

            p.age = 22;

            p.name = "Huang Ailing";

            p.height = 165.5f;

            p.homeAddress = "Chongqing";

            Person p1 = new Person();

            p1.age = 22;

            p1.name = "Yu Shuang";

            p1.height = 170.5f;

            p1.homeAddress = "Chongqing";

            Student s = new Student();

            s.name = "Brother Volcano";

            s.number = 1;

            s.age = 22;

            Student s1 = new Student();

            s1.name = "Teacher Tang";

            s1.number = 2;

            s1.age = 22;

            s.deskmate = s1;

            s1.deskmate = s;

            Class c = new Class();

            c.name = "Class Four";

            c.capacity = 9999;

            c.students = new Student[] { s, s1 };*/

            #endregion

 

            #region Exercise 5

            /*Person p = new Person();

            p.age = 10;

            Person p2 = new Person();

            p2.age = 20;

            Console.WriteLine(p.age);*/

            //How much is p.age?

            //10

            #endregion

 

            #region Exercise 6

            /*Person p = new Person();

            p.age = 10;

            Person p2 = p;

            p2.age = 20;

            Console.WriteLine(p.age);*/

            //How much is p.age?

            //20

            #endregion

 

            #region Exercise 7

            /*Student s = new Student();

            s.age = 10;

            int age = s.age;

            age = 20;

            Console.WriteLine(s.age);*/

            //How much is s.age?

            //10

            #endregion

 

            #region Exercise 8

            /*Student s = new Student();

            s.deskmate = new Student();

            s.deskmate.age = 10;

            Sudent s2 = s.deskmate;

            s2.age = 20;

            Console.WriteLine(s.deskmate);*/

            //How much is s.deskmate.age?

            //20

            #endregion

        }

    }

}

 

Guess you like

Origin blog.csdn.net/weixin_61541885/article/details/128745093