コード例 #1
0
        public void Study()
        {
            // great link for reviewing and writing these variables https://www.geeksforgeeks.org/c-sharp-types-of-variables/
            Console.WriteLine("Non-, also called Instance variables, are declared in the class block, outside of the method or constructor.");
            Console.WriteLine("They exist when the object is created, and destroyed when a new object create it. Look at StaticVsInstance line 16.");
            ObjectPrinciples op1 = new ObjectPrinciples("", "", "");
            ObjectPrinciples op2 = new ObjectPrinciples("", "", "");

            op1.InstanceVariable = 15;
            Console.WriteLine("The original value of InstanceVariable was 111, it is now: " + op1.InstanceVariable);
            Console.ReadLine();
            op1.InstanceVariable = 6;
            Console.WriteLine("The Instance Variable has been redfined, it is now: " + op1.InstanceVariable);
            Console.WriteLine(ObjectPrinciples.StaticVar);
            ObjectPrinciples.StaticVar = 5;
            Console.WriteLine("This is the StaticVar from ObjectPrinciples redefined " + ObjectPrinciples.StaticVar);
            StaticVariable = 88;
            Console.WriteLine(StaticVariable + " This was the last instance of the static variable that was printed. The next line shows us calling the staticVar from ObjectPrinciples and redefining it in the next");
            Console.ReadLine();
            Console.WriteLine("Practice writing and calling static variables and instances in Notepad (call one bird and the other one food if needed).");
            Console.ReadLine();
            Console.Clear();//this is why you have methods, to make the code look neater than this ugly line calling going on.
            Console.WriteLine("Static means the method can be called without an instance of the object. Example the Continue() on Program.cs or any Math.Round (math being the class).");
            Console.ReadLine();
            Console.WriteLine("Where as everytime I wanted to call the Study() from my other classes, I had to make an instance of the class on the Program.cs page.");
            Console.ReadLine();
            Console.WriteLine("Practice writing the following in Notepad: static fields, instance fields. Then try calling those fields in a static method and again in a instance method (remember methods you created are by default instance ones).");
        }
コード例 #2
0
        public void Studyi()
        {
            ObjectPrinciples principles = new ObjectPrinciples("", "", "");//this was to create an instance so I can kidnap-err use the NotSafeVariable.

            Console.WriteLine("There are a lot of modifiers. In fact, abstract, override, and static are modifiers. But we only care about 3 modifiers:");
            bool run = true;

            while (run)
            {
                Console.WriteLine("1.) Public");//I should have done a foreach loop for this ugh.
                Console.WriteLine("2.) Protected");
                Console.WriteLine("3.) Private");
                Console.WriteLine("Which one would you like to learn about?");
                int userInput = int.Parse(Console.ReadLine());
                if (userInput == 1)
                {
                    Console.WriteLine("Public means you can access it from EVERYWHERE. Watch this (line 26 on AccessModifiers.cs)");
                    Console.ReadLine();
                    Console.WriteLine((principles.NotSafeVariable));

                    Console.WriteLine("I was able to grab the NotSafeVariable string from ObjectPrinciple.cs.");
                }
                else if (userInput == 2)
                {
                    Console.WriteLine("Protected means access is limited to within the class and any class that inherits (its children) the class.");
                    Console.WriteLine("Look on line 35 on AccessModifiers.cs. If you uncomment the line, an error will appear saying you can't access the CantTouchThis variable. That is because it is protected.");
                    //Console.WriteLine(principles.CantTouchThis);
                }
                else if (userInput == 3)
                {
                    Console.WriteLine(ForAccessModifiersOnly);
                    Console.WriteLine("Private means access is only limited to within the class. I can't be passed anywhere outside the class it was created.");
                }
                Console.WriteLine("Would you like to Continue? (y/n)");
                string answer = Console.ReadLine().ToLower();
                if (answer == "y")
                {
                    run = true;
                }
                else if (answer == "n")
                {
                    run = false;
                }
            }
        }
コード例 #3
0
        static void Main(string[] args)
        {
            List <string> Unit3 = new List <string>();

            Unit3.Add("The Four Object Oriented Principles");
            Unit3.Add("Classes and Objects");
            Unit3.Add("Constructors");
            Unit3.Add("Properties");
            Unit3.Add("Abstract Methods, Abstract Classes, and Interfaces");
            Unit3.Add("Access modifiers");
            Unit3.Add("Static Methods and Variables vs. Instance Methods and Variables");
            Unit3.Add("Base Keyword");
            Unit3.Add("This Keyword");
            bool run = true;

            while (run)
            {
                Console.Clear();
                Console.WriteLine("Please select a number you would like to review.");
                int index = 0;
                foreach (string subject in Unit3)
                {
                    Console.WriteLine(index + ".) " + subject);
                    index++;
                }

                int userInput = int.Parse(Console.ReadLine());
                if (userInput == 0)
                {
                    Console.Clear();
                    ObjectPrinciples op = new ObjectPrinciples("", "", ""); //this is creating the object called ObjectPrinciples stored in the variable op. Notice I have 3 empty strings? Why did I need 3 strings?
                    op.Study();                                             //this is the public methods on the object, we called it using the variable op.
                }
                else if (userInput == 1)
                {
                    Console.Clear();
                    ClassesObjects co = new ClassesObjects();
                    co.Study(); //I could have made my life easier by making an abstract class or interface so I don't have to write Study() method on every class... 5 minutes later... I wrote AbstractParent to give inheritance to children.
                }
                else if (userInput == 2)
                {
                    Console.Clear();
                    Constructors con = new Constructors();
                    con.Study();
                }
                else if (userInput == 3)
                {
                    Console.Clear();
                    Properties pro = new Properties(); //the class Properties.cs was made a child of an Interface class called IIinterfaceClass.s
                    pro.Studyi();
                    pro.GetNumber    = 5;              //I purposely left this error here to show off Get set, if you mouse over the error, it will tell you GetNumber is a read only from the Properties.cs class.
                    pro.GetSetNumber = 12;

                    Console.WriteLine("GetNumber: " + pro.GetNumber + "\t" + "Get SetNumber " + pro.GetSetNumber);
                }
                else if (userInput == 4)
                {
                    Console.Clear();
                    AbstractMethodsClassesInterfaces amci = new AbstractMethodsClassesInterfaces();
                    amci.Study();
                    amci.Interfaces();

                    Console.Clear();
                    Console.WriteLine("Surprise! I am able to call ConcreteVirtual on the Program.cs because its public and not protected modifier. (change it on the AbstractParent class to protected and see what happens on Program.cs Line 67)");
                    amci.ConcreteVirtual();
                }
                else if (userInput == 5)
                {
                    Console.Clear();
                    AccessModifiers am = new AccessModifiers();
                    am.Studyi();
                }
                else if (userInput == 6)
                {
                    Console.Clear();
                    StaticVsInstance svi = new StaticVsInstance();
                    svi.Study();
                }
                else if (userInput == 7)
                {
                    Console.Clear();
                    This bs = new This();
                    bs.Study();
                }
                else if (userInput == 8)
                {
                    Console.Clear();
                    This bs = new This();//its easy to compare them this way.
                    bs.Study();
                }
                run = Continue();
            }
        }