본문 바로가기
CSharp/CSharp 문법

클래스

by MonoSoft 2024. 12. 3.
728x90
반응형

c# 클래스

using System.Drawing;
using System.Net.Http.Headers;
using System.Timers;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using System.Runtime.CompilerServices;
 
 
namespace _07_Class
{
     
 
    readonly struct RGBColor
    {
        public readonly byte R;
        public readonly byte G;
        public readonly byte B;
 
        public RGBColor(byte r, byte g, byte b)
        {
            R = r;
            G = g;
            B = b;
        }
    }
 
 
    struct Point3D
    {
        public int X;
        public int Y;
        public int Z;
 
        public Point3D(int X, int Y, int Z)
        {
            this.X = X;
            this.Y = Y;
            this.Z = Z;
        }
 
        public override string ToString()
        {
            return string.Format($"{X},{Y},{Z}");
        }
    }
 
 
    // 확장 메서드를 정의하는 정적 클래스
    public static class StringExtensions
    {
        // 문자열을 뒤집는 확장 메서드
        public static string ReverseString(this string input)
        {
            if (string.IsNullOrEmpty(input))
                return input;
 
            char[] charArray = input.ToCharArray();
            Array.Reverse(charArray);
            return new string(charArray);
        }
    }
 
    public static class IntegerExtension
    {
        public static int Square(this int myInt)
        {
            return myInt * myInt;
        }
 
        public static int Power(this int myInt, int exponent)
        {
            int result = myInt;
            for (int i = 0; i < exponent; i++)
            {
                result = result * myInt;
            }
            return result;
        }
    }   
 
    internal class Program
    {
 
        class Base
        {
            public void MyMethod()
            {
                Console.WriteLine("Base.MyMEthod()");
            }
        }
 
        class Derived : Base 
        { 
            public new void MyMethod()
            {
                Console.WriteLine("Derived.MyMethod()");
            }
        }
 
        class Configuration
        {
            List<ItemValue> listConfig = new List<ItemValue>();
 
            public void SetConfig(string item, string value)
            {
                ItemValue iv = new ItemValue();
                iv.SetValue(this, item, value);
            }
 
            public string GetConfig(string item)
            {
                foreach (ItemValue iv in listConfig)
                {
                    if (iv.GetItem() == item)
                    {
                        return iv.GetValue();
                    }
                }
 
                return "";
            }
 
            private class ItemValue
            {
                private string item;
                private string value;
 
                public void SetValue(Configuration config, string item, string value)
                {
                    this.item = item;
                    this.value = value;
 
                    bool found = false;
 
                    for (int i = 0; i < config.listConfig.Count; i++)
                    {
                        if (config.listConfig[i].item == item)
                        {
                            config.listConfig[i] = this;
                            found = true;
                            break;
                        }                        
                    }
 
                    if (found == false)
                        config.listConfig.Add(this);
                }
 
                public string GetItem()
                { return item; }
 
                public string GetValue() 
                { return value; }
            }
        }
 
 
        partial class PartialMyClass
        {
            public void Mehthod1()
            {
                Console.WriteLine("Method1");
            }
 
            public void Mehthod2()
            {
                Console.WriteLine("Method2");
            }
        }
 
        partial class PartialMyClass
        {
            public void Mehthod3()
            {
                Console.WriteLine("Method3");
            }
 
            public void Mehthod4()
            {
                Console.WriteLine("Method4");
            }
        }
 
        /*
        class ArmorSuite
        {
            public virtual void Initialize()
            {
                Console.WriteLine("Armored");
            }
        }
 
        class IronMan : ArmorSuite
        {
            public override void Initialize()
            {
                base.Initialize();
                Console.WriteLine("Repulsor Rays Armed");
            }
        }
 
        class WarMachine : ArmorSuite
        {
            public override void Initialize()
            {
                base.Initialize();
                Console.WriteLine("Dowuble-BArrel Cannons Armed");
                Console.WriteLine("Micro-Rocked Launcher Armed");
            }
        }
 
 
        
           class Cat
           {
               public string Name;
               public string Color;
 
               public void MEow()
               {
                   Console.WriteLine($"{Name} : 야옹");
               }
           }
        */
 
        /*
        class Cat
        {
            public Cat()
            {
                Name = "";
                Color = "";
            }
 
            public Cat(string _Name, string _Color)
            {
                Name = _Name;
                Color = _Color;
            }
 
            ~Cat()
            {
                Console.WriteLine($"{Name}:잘가");
            }
 
            public string Name;
            public string Color;
 
            public void Meow()
            {
                Console.WriteLine($"{Name}:야옹");
            }
        }
        */
 
 
        /*
        class MyClass
        {
            public int MyField1;
            public int MyField2;
 
            public MyClass DeepCopy()
            {
                MyClass newCopy = new MyClass();
                newCopy.MyField1 = this.MyField1;
                newCopy.MyField2 = this.MyField2;
 
                return newCopy;
            }
        }
        */
 
        /*
        class Base
        {
            protected string Name;
            public Base(string name)
            {
                this.Name = name;
                Console.WriteLine($"{this.Name}.Base()");
            }
 
            ~Base()
            {
                Console.WriteLine($"{this.Name}.~Base()");
            }
 
            public void BaseMethod()
            {
                Console.WriteLine($"{Name}.BaseMethod()");
            }
        }
 
        class Derived : Base
        {
            public Derived(string name) : base(name)
            {
                Console.WriteLine($"{this.Name}.Derived()");
            }
 
            ~Derived()
            {
                Console.WriteLine($"{this.Name}.~Derived");
            }
 
            public void DerivedMethod()
            {
                Console.WriteLine($"{Name}.DerivedMethod()");
            }
        }
 
        
        class Employee
        {
            private string Name = "";
            private string Position = "";
 
            public void SetName(string Name)
            {
                this.Name = Name;
            }
 
            public string GetName()
            {
                return Name;
            }
 
            public void SetPosition(string Position)
            {
                this.Position = Position;
            }
 
            public string GetPosition()
            {
                return Position;
            }
 
        }
 
        class MyClass
        {
            int a, b, c;
 
            public MyClass()
            {
                this.a = 5425;
                Console.WriteLine("MyClass()");
            }
 
            public MyClass(int b) : this()
            {
                this.b = b;
                Console.WriteLine($"MyClass({b})");
            }
 
            public MyClass(int b, int c) : this()
            {
                this.c = c;
                Console.WriteLine($"MyClass({b},{c})");
            }
 
            public void PrintFields()
            {
                Console.WriteLine($"a:{a},b:{b}, c:{c}");
            }
        }
 
        class WaterHeadter
        {
            protected int temperature;
 
            public void SetTemperature(int temperature)
            {
                if (temperature < -5 || temperature > 42)
                {
                    throw new Exception("Out of temperature range");
                }
 
                this.temperature = temperature;
            }
 
            internal void TurnOnWater()
            {
                Console.WriteLine($"Turn on water : {this.temperature}");
            }
 
        }
        */
 
        class Mammal
        {
            public void Nurse()
            {
                Console.WriteLine("Nurse()");
            }
        }
 
        class Dog : Mammal
        {
            public void Bark()
            {
                Console.WriteLine("Bark()");
            }
        }
 
        class Cat : Mammal
        {
            public void Meow()
            {
                Console.WriteLine("Meow()");
            }
        }
 
 
        static void Main(string[] args)
        {
            /*
            Cat kitty = new Cat();
            kitty.Color = "하얀색";
            kitty.Name = "키티";
            kitty.MEow();
            Console.WriteLine($"{kitty.Name}:{kitty.Color}");
 
            Cat nero = new Cat();
            kitty.Color = "검은색";
            kitty.Name = "네로";
            kitty.MEow();
            Console.WriteLine($"{kitty.Name}:{kitty.Color}");
            */
 
            /*
            Cat kitty = new Cat("키티", "하얀색");
            kitty.Meow();
            Console.WriteLine($"{kitty.Name}:{kitty.Color}");
 
            Cat nero = new Cat("네로", "검은색");
            nero.Meow();
            Console.WriteLine($"{nero.Name}:{nero.Color}");
            */
 
            /*
            Console.WriteLine("Shallow Copy");
 
            {
                MyClass source = new MyClass();
                source.MyField1 = 10;
                source.MyField2 = 20;
 
                MyClass target = source;
                target.MyField2 = 30;
 
                Console.WriteLine($"{source.MyField1} {source.MyField2}");
                Console.WriteLine($"{target.MyField1} {target.MyField2}");
            }
 
            Console.WriteLine("Deep Copy");
 
            {
                MyClass source = new MyClass(); 
                source.MyField1 = 10;
                source.MyField2 = 20;
 
                MyClass target = source.DeepCopy();
                target.MyField2 = 30;
 
                Console.WriteLine($"{source.MyField1} {source.MyField2}");
                Console.WriteLine($"{target.MyField1} {target.MyField2}");
            }
            */
 
            /*
            Employee pooh = new Employee();
            pooh.SetName("Pooh");
            pooh.SetPosition("Waiter");
            Console.WriteLine($"{pooh.GetName()} {pooh.GetPosition()}");
 
            Employee tigger = new Employee();
            tigger.SetName("Tigger");
            tigger.SetPosition("Cleaner");
            Console.WriteLine($"{tigger.GetName()} {tigger.GetPosition()}");
            */
 
            /*
            MyClass a = new MyClass();
            a.PrintFields();
            Console.WriteLine();
 
            MyClass b = new MyClass(1);
            b.PrintFields();
            Console.WriteLine();
 
            MyClass c = new MyClass(10,20);
            c.PrintFields();
            */
 
            /*
            try
            {
                WaterHeadter heater = new WaterHeadter();
                heater.SetTemperature(20);
                heater.TurnOnWater();
 
                heater.SetTemperature(-2);
                heater.TurnOnWater();
 
                heater.SetTemperature(50);
                heater.TurnOnWater();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);                
            }
            */
 
            /*
            Base a = new Base("a");
            a.BaseMethod();
 
            Derived b = new Derived("b");
            b.BaseMethod();
            b.DerivedMethod();
            */
 
            /*
            Mammal mammal = new Dog();
            Dog dog;
 
            if (mammal is Dog)
            {
                dog = (Dog)mammal;
                dog.Bark();
            }
 
            Mammal mammal2 = new Cat();
 
            Cat cat = mammal2 as Cat;
            if (cat != null)
            {
                cat.Meow();
 
                Cat cat2 = mammal as Cat;
                if (cat2 != null)
                    cat2.Meow();
                else
                    Console.WriteLine("cat2 is not a Cat");
            }
            */
 
            /*
            Console.WriteLine("Creating ArmorSuite...");
            ArmorSuite armorsuite = new ArmorSuite();
            armorsuite.Initialize();
 
            Console.WriteLine("/nCreating IronMan...");
            ArmorSuite ironman = new IronMan();
            ironman.Initialize();
 
            Console.WriteLine("/nCreating WarMachine....");
            ArmorSuite warmachine = new WarMachine();
            warmachine.Initialize();
            */
 
            /*
            Base baseObj = new Base();
            baseObj.MyMethod();
 
            Derived derivedObj = new Derived();
            derivedObj.MyMethod();
 
            Base baseOrDerived = new Derived();
            baseOrDerived.MyMethod();
            */
 
            /*
            Configuration config = new Configuration();
            config.SetConfig("Version","V 5.0");
            config.SetConfig("Size", "655,324 KB");
 
            Console.WriteLine(config.GetConfig("Version"));
            Console.WriteLine(config.GetConfig("Size"));
 
            config.SetConfig("Version", "V 5.0.1");
            Console.WriteLine(config.GetConfig("Version"));
            */
 
            /*
            PartialMyClass partialMyClass = new PartialMyClass();
            partialMyClass.Mehthod1();
            partialMyClass.Mehthod2();
            partialMyClass.Mehthod3();
            partialMyClass.Mehthod4();
            */
 
            /*
            Console.WriteLine($"3^2 : {3.Square()}");
            Console.WriteLine($"3^2 : {3.Power(4)}");
            Console.WriteLine($"3^2 : {2.Power(10)}");
 
            string original = "Hello, World!";
            string reversed = original.ReverseString(); // 확장 메서드 사용
 
            Console.WriteLine($"Original: {original}");
            Console.WriteLine($"Reversed: {reversed}");
            */
 
            /*
            Point3D p3d1;
            p3d1.X = 10;
            p3d1.Y = 20;
            p3d1.Z = 40;
 
            Console.WriteLine(p3d1.ToString());
 
            Point3D p3d2 = new Point3D(100,200,300);
            Point3D p3d3 = p3d2;
            p3d2.Z = 400;
 
            Console.WriteLine(p3d2.ToString());
            Console.WriteLine(p3d3.ToString());
            */
            /*
            RGBColor Red = new RGBColor(255, 0, 0);
            //Red.G = 100; //컴파일 에러 
            RGBColor myColor = new RGBColor(Red.R, 100, Red.B);
            */
 
            /*
            //명명되지 않은 튜플
            var a = ("슈퍼맨", 9999);
            Console.WriteLine($"{a.Item1},{a.Item2}");
 
            //명명된 튜플
            var b = (Name:"홍길동", Age:17);
            Console.WriteLine($"{b.Name},{b.Age}");
 
            //분해
            var (name, age) = b;//(var name, var age) = b;
            Console.WriteLine($"{name},{age}");
 
            //분해2
            var (name2, age2) = ("김말자", "34");
            Console.WriteLine($"{name2}, {age2}");
 
            //명명된 튜플 = 명명되지 않은 튜플
            b = a;
            Console.WriteLine($"{b.Name}, {b.Age}");
            */
 
 
 
 
 
 
        }
    }
}
728x90
반응형

'CSharp > CSharp 문법' 카테고리의 다른 글

추상클래스  (0) 2024.12.12
메소드 ( Method)  (0) 2024.08.16
코드의 흐림제어  (0) 2024.08.01
C# 문법  (0) 2024.07.01
C# 강의 메모  (0) 2022.09.28

댓글