By GokiSoft.com| 14:59 21/05/2021|
C Sharp

[Share Code] Tìm hiểu Tính chất trong lập trình OOP - JSON Newtonsoft - Đọc ghi File - Lập trình C#


Nội dung kiến thức học: - Tính chất trong lập trình hướng đối tượng - Tính bao đóng => DONE - Tính kế thừa => DONE - Tính đa hình => DONE - Tính trừu tượng => DONE - Interface - JSON trong lập trình C# =============================================== Xay dung 1 chuong trinh quan ly xe co - Xe may Thuoc tinh: Ten, Ngay Nhap, Color Thuoc tinh: Input Display GetWheelCount -> 2 banh xe - Car Thuoc tinh: Ten, Ngay Nhap, Color Thuoc tinh: Input Display GetWheelCount -> 4 banh - Truck Thuoc tinh: Ten, Ngay Nhap, Color Thuoc tinh: Input Display GetWheelCount -> 4 banh - Nhan xet: Xe May, Car, Truck => Vehicle Thuoc tinh: Ten, Ngay Nhap, Color Thuoc tinh: Input Display GetWheelCount -> ko biet dc so banh xe Xây dựng 1 chức năng -> Sử dụng 1 List (library & source code) -> Quản lý hành running() -> của các đội tượng khác Car, Bike, Truck, People, Cat -> Interface (hành động chung => của các nhóm đối tượng khác nhau)




#IRunning.cs


using System;
namespace Lesson04.Inf
{
    public interface IRunning
    {
        void onRunning();

        //bool checkRunning();
    }
}


#IStop.cs


using System;
namespace Lesson04.Inf
{
    public interface IStop
    {
        void Stop();
    }
}


#Bike.cs


using System;
namespace Lesson04.Modals
{
    public class Bike : Vehicle
    {
        public Bike()
        {

        }

        public Bike(string name, string importedDate, string color) : base(name, importedDate, color)
        {
        }

        public override int GetWheelCount()
        {
            return 2;
        }
    }
}


#Vehicle.cs


using System;
using Lesson04.Inf;

namespace Lesson04.Modals
{
    public abstract class Vehicle : IRunning, IStop
    {
        public string Name { get; set; }
        public string ImportedDate { get; set; }
        public string Color { get; set; }

        public Vehicle()
        {
        }

        public Vehicle(string name, string importedDate, string color)
        {
            this.Name = name;
            this.ImportedDate = importedDate;
            this.Color = color;
        }

        public void Input()
        {
            Console.WriteLine("Viet chuong trinh nhap du lieu ....");
        }

        public void Display()
        {
            Console.WriteLine("Hien thi thong tin du lieu ...");
        }

        /**
         * Khi noi ve 1 xe chung chung -> ko xac dinh dc so banh xe chuan duoc -> phu thuoc vao tung loai xe cu the.
         */
        public abstract int GetWheelCount();

        public void onRunning()
        {
            Console.WriteLine("Vehicle is running...");
        }

        public void Stop()
        {
            throw new NotImplementedException();
        }
    }
}


#Truck.cs


using System;
namespace Lesson04.Modals
{
    public class Truck : Vehicle
    {
        public override int GetWheelCount()
        {
            return 4;
        }
    }
}


#People.cs


using System;
using Lesson04.Inf;

namespace Lesson04.Modals
{
    public class People : IRunning, IStop
    {
        public People()
        {
        }

        public void onRunning()
        {
            Console.WriteLine("People is running");
        }

        public void Stop()
        {
            throw new NotImplementedException();
        }
    }
}


#Cat.cs


using System;
using Lesson04.Inf;

namespace Lesson04.Modals
{
    public class Cat : IRunning, IStop
    {
        public Cat()
        {
        }

        public void onRunning()
        {
            Console.WriteLine("Cat is running");
        }

        public void Stop()
        {
            throw new NotImplementedException();
        }
    }
}


#Car.cs


using System;
namespace Lesson04.Modals
{
    public class Car : Vehicle
    {
        public override int GetWheelCount()
        {
            return 4;
        }
    }
}


#Program.cs


using System;
using Lesson04.Modals;
using System.Collections.Generic;
using Lesson04.Inf;

namespace Lesson04
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            //Test();

            List<Bike> bikes = new List<Bike>();
            bikes.Add(new Bike("A", "A", "A"));
            bikes.Add(new Bike("B", "B", "B"));
            bikes.Add(new Bike("C", "C", "C"));

            //JSON (encode & decode)
            //Chuyen Array -> JSON string
            string content = Newtonsoft.Json.JsonConvert.SerializeObject(bikes);
            Console.WriteLine(content);
            //Save file text
            System.IO.File.WriteAllText(@"bikes.json", content);
            //System.IO.File.AppendAllText(@"bikes.json", content);

            //Chuyen JSON sstring -> Array
            //string content2 = "[{\"Name\":\"A\",\"ImportedDate\":\"A\",\"Color\":\"A\"},{\"Name\":\"B\",\"ImportedDate\":\"B\",\"Color\":\"B\"},{\"Name\":\"C\",\"ImportedDate\":\"C\",\"Color\":\"C\"}]";
            //Read file text
            string content2 = System.IO.File.ReadAllText(@"bikes.json");
            List<Bike> list2 = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Bike>>(content2);
            foreach(Bike b in list2)
            {
                Console.WriteLine("Name: {0}", b.Name);
            }
        }

        static void Test()
        {
            Bike bike = new Bike();
            Car car = new Car();
            Truck truck = new Truck();

            Console.WriteLine("Bike: {0}, Car: {1}, Truck: {2}",
                bike.GetWheelCount(), car.GetWheelCount(), truck.GetWheelCount());

            List<IRunning> list = new List<IRunning>();

            IRunning obj = new Bike();//obj -> tro vao 1 vung nho A
            list.Add(obj); //list[0] tro vao vung nho A
            obj = new Car(); //obj -> tro vao 1 vung nho B
            list.Add(obj);//list[1] tro vao vun nho B -> list[0] tro vao vung nho A

            list.Add(bike);
            list.Add(car);
            list.Add(truck);
            list.Add(new People());
            list.Add(new Cat());

            foreach (IRunning running in list)
            {
                running.onRunning();
            }
        }
    }
}


Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Do Trung Duc [T2008A]
Do Trung Duc

2021-05-21 15:14:29



using System;
using System.Collections.Generic;
using System.Text;
using Lesson4.JSON.StudentManager;

namespace Lesson4.JSON.StudentManager
{
    class ClassRoom
    {
        public string Name { get; set; }
        public string Address { get; set; }

        public List<Student> StudentList { get; set; }

        public ClassRoom() { }

        public void Input()
        {
            Console.WriteLine("Nhap ten lop: ");
            Name = Console.ReadLine();

            Console.WriteLine("Nhap dia chi lop: ");
            Address = Console.ReadLine();
        }


    }
}