示例#1
0
        static void Main(string[] args)
        {
            Person john = new Person("John", 32);

            john.Talk("Classes are awosome!");

            Person bob = new Person();

            bob.Name = "Bob";
            bob.Age  = 33;
            bob.Talk("Yes, they are!");

            Person jane = new Person()
            {
                Name = "Jane",
                Age  = 23
            };

            jane.Talk("This approach is called object initializer");

            Product product1 = new Product()
            {
                Name  = "PRODUCT NAME",
                Code  = 123123123,
                Price = 25
            };

            product1.Buy();
            Console.ReadLine();
        }
示例#2
0
        static void Main(string[] args)
        {
            // Creating an empty person object (using the parameterless constructor)
            Person bob = new Person();

            bob.Name = "Bob Bobsky";
            bob.Age  = 34;
            //bob.SSN = 3453254252; // We can't access this. It's private
            bob.Talk("Hey there");
            //bob.GenerateSSN(); // We can't call this. It's private

            // Creating a person object using the second constructor with parameters
            Person jill = new Person("Jill Wayne", 29); // we don't need to set the properties

            jill.Talk("Hello!");

            // Creating a product because we have access to it since we are in the same namespace
            Product car = new Product();

            car.Name  = "Car";
            car.Code  = 12314;
            car.Price = 6500.99;

            car.Buy(5000);
            car.Buy(10000);

            Console.ReadLine();
        }
示例#3
0
        static void Main(string[] args)
        {
            Person person = new Person();

            person.Name = "Viktor";
            person.Age  = 31;

            person.Talk("Hello there!");

            Person person2 = new Person("Angela", 25);

            person2.Talk("Hi there!");


            Product car = new Product()
            {
                Code   = 11902,
                Name   = "Car",
                Price  = 25500,
                Person = person
            };

            Console.WriteLine(car.Name);
            Console.WriteLine(car.Price);
            Console.WriteLine(car.Person.Name);

            car.Buy(35000);



            Console.ReadLine();
        }
示例#4
0
        static void Main(string[] args)
        {
            // Creating an empty person object ( using the parameterless constructor )
            Person bob = new Person();

            bob.Name = "Bob Bobsky";
            bob.Age  = 34;
            //bob.SSN = 3453254252; // We can't access this. It's private
            bob.Talk("Hey there");
            //bob.GenerateSSN(); // We can't call this. It's private

            // Creating an empty person object and immediately giving it values to the properties
            Person bill = new Person()
            {
                Name = "Bill Wurtz", Age = 31
            };

            bill.Talk("Good day people");

            // Creating a person object using the second construvtor with parameters
            Person jill = new Person("Jill Wayne", 29); // we don't need to set the properties

            jill.Talk("Hello!");

            // Creating a product because we have access to it since we are in the same namespace
            Product car = new Product()
            {
                Name = "Car", Code = 12314, Price = 6500.99
            };

            car.Buy(5000);
            car.Buy(10000);

            // Creating an annonimous object
            // We cant add methods in these objects, just properties
            var annonObject = new
            {
                Name     = "Random Object",
                Quantity = 25
            };

            Console.WriteLine(annonObject.Name);

            Console.ReadLine();
        }