1. Options 진입하기

오른쪽 'X'자 표시 닫는거 아래쪽 어딘가에 보면 톱니바퀴가 있다.

 

2. 'Keyboard' 검색

여기서 원하는대로 바꾸면 된다.

 

일단 다른 키는 차차 바꾸면 될 것 같고...

맥 쓰다 윈도우 쓰면 젤 불편한게... 앱마다 설정 들어가는 메뉴가 여기저기 멋대로 흩어져 있고 정해진 단축키가 없다는거다... 이거 정말 너무 불편...

1 ) 그래서 설정 진입과 주석 키는 우선 다음과 같이 설정해주었다.

'Tools.OptionsandSettings'항목이다. => 키 리맵핑을 다 해두었기 때문에 이렇게 하면 한/영키가 command키에 위치해 있으므로 맥에서 command + ',' 쓰던거를 그대로 쓸 수 있다.

변경할 때는 반드시 'Press shortcut keys'에 단축키를 입력한 다음 옆에 'Assign'을 클릭 후 'OK'를 눌러줘야한다.

 

2 ) 주석은 위 아래 스샷처럼 변경하였다.

'Edit.ToggleLineComment'항목이며, 마찬가지로 command + '/' 쓰던거를 그대로 쓸 수 있다.

 

3 ) 탭 닫기

'Window.CloseDocumentWindow'항목이다. command + 'w'처럼 작동

이게 단축키가 중복되어 저장되서 Assign을 해도 사용이 안 된다. 즉, 하나의 기능에 여러개의 단축키가 저장되는 문제로 먼저 Shortcuts for seelected command에서 Remove를 해서 지우고 등록한다.

Visual Studio의 단축키 변경은 문제가 많다... 아무리 바꿔도 원래 단축키로 작동하는 것들이 몇 개 있다... 그 예가 바로 이것..

 

4 ) 코드 자동 줄맞춤

'Edit.FormatSelection'항목이다. command + 'K, F'처럼 작동

 

5 ) 클래스 파일 생성

'Project.AddNewItem'항목이다. command + 'N'처럼 작동

 

6 ) 자동완성

'QuickActions'로 검색하자. option + space 기준으로 적용

 

 

 

 

맥 유저를 위한 키 리매핑은 다음 글을 참고한다.

2020/05/15 - [개발자/용어... 그 외의 것들...] - 윈도우 맥처럼

 

윈도우 맥처럼

만약 최신 노트북이라 트랙패드 감도 좋고 제스쳐가 지원된다면 트랙패드 개선은 건너 뛴다. 이 경우 트랙패드의 확대/축소가 Control + 마우스 휠을 이용한 확대/축소인 경우 마우스 스크롤 뒤집

greendreamtrre.tistory.com

 

클래스를 선언할 때 abstract를 명시하는 것 말고도 인터페이스가 있다. 뭐 자바에서와 같은 개념이다. 인터페이스는 그 자체가 완전히 추상화 된 클래스다.

An interface is a completely "abstract class", which can only contain abstract methods and properties (with empty bodies)

// interface
interface Animal {
  void animalSound(); // interface method (does not have a body)
  void run(); // interface method (does not have a body)
}

 

클래스 상속은 1개만 가능하고, 2개 이상을 상속하려면 인터페이스를 통한 추상화 상속으로만 가능하다. 개념은 자바와 동일하다. 단지 문법만 다르다.

 

1. Java

Java Class Inheritance (1개) -> extends를 사용

Java Inteface (2개 이상) -> implements를 사용

 

2. C#

C# Class Inheritance (1개) -> : 를 사용

C# Interface (2개 이상) -> : 를 사용

Abstraction은 restricted (축소된) 클래스와 메소드를 말한다.

1. Abstract class는 instance화 할 수 없다.
  -> inheritance(상속)을 해야 사용할 수 있다. 상속 받은 클래스는 instance화를 할 수 있음.

2. Abstract method는 abstract class 내에서만 사용할 수 있다.
  -> 위 1번에서 클래스를 상속하면서 메소드를 overriding(오버라이딩) 해야 사용할 수 있다.

 

// Abstract class
abstract class Animal
{
  // Abstract method (does not have a body)
  public abstract void animalSound();
  // Regular method
  public void sleep()
  {
    Console.WriteLine("Zzz");
  }
}


// Sub class (inherit from Animal)
class Pig : Animal
{
  public override void animalSound()
  {
    // The body of animalSound() is provided here
    Console.WriteLine("The pig says: wee wee");
  }
}


class Program
{
  static void Main(string[] args)
  {
    Pig myPig = new Pig(); // Create a Pig object
    myPig.animalSound();  // Call the abstract method
    myPig.sleep();  // Call the regular method
  }
}


결과 :
The pig says: wee wee
Zzz

설명 :
1 ) Animal 클래스는 abstract 클래스다. animalSound( ) 메소드는 abstract 메소드다.
2 ) Animal 클래스를 상속해 Pig 클래스를 만들고, 그러면서 animalSound( ) 메소드를 public 으로 override 했다.
3 ) Program 클래스에서 main 메소드가 프로그램을 시작한다. 
4 ) myPig라는 이름으로 Pig 클래스를 instance화 한다. (Pig 클래스는 Animal 클래스를 상속 받아 만들어 진 abstract가 아닌 일반 클래스라 instance가 가능하다.)
5 ) myPig의 animalSound( ) 메소드를 실행한다. (Pig 클래스의 animalSound( ) 메소드는 public으로 override 되었기 때문에 사용이 된다.)
6 ) myPig의 sleep( ) 메소드는 Pig 클래스에서 override는 하지 않았지만 abstract 메소드가 아닌 public 으로 선언되었기 때문에 Pig 클래스에 자동으로 상속 되어 사용이 된다.

 

추상 클래스를 만드는 또 다른 방법

2021/01/22 - [개발자/.NET] - C# (C Sharp) Inheritance 상속

 

C# (C Sharp) Inheritance 상속

1. Namespace(=Package)에 각 클래스 파일로 분류한 프로젝트 구조를 그대로 보면 Vehicle.cs using System; namespace MyApplication { class Vehicle // Base class { public string brand = "Ford"; // Vehicle..

greendreamtrre.tistory.com

2021/01/22 - [개발자/.NET] - C# (C Sharp) Interface (인터페이스 추상화) 자바와 차이

 

C# (C Sharp) Interface (인터페이스 추상화) 자바와 차이

클래스를 선언할 때 abstract를 명시하는 것 말고도 인터페이스가 있다. 뭐 자바에서와 같은 개념이다. 인터페이스는 그 자체가 완전히 추상화 된 클래스다. An interface is a completely "abstract class", wh

greendreamtrre.tistory.com

 

Tag. 추상 메소드, c# 추상 클래스, c sharp 추상 클래스, 씨샵 추상 클래스, c# 추상 메소드, c sharp 추상 메소드, 씨샵 추상 메소드

1. Namespace(=Package)에 각 클래스 파일로 분류한 프로젝트 구조를 그대로 보면

Vehicle.cs

using System;

namespace MyApplication
{
  class Vehicle  // Base class
  {
    public string brand = "Ford";  // Vehicle field
    public void honk()             // Vehicle method 
    {
      Console.WriteLine("Tuut, tuut!");
    }
  }
}

 

Car.cs

using System;

namespace MyApplication
{
  class Car : Vehicle  // Derived class
  {
    public string modelName = "Mustang";  // Car field
  }
}

Result:
Tuut, tuut!
Ford Mustang

Vehicle이라는 super class를 상속 받은 Car이라는 child class

참고 : 자바에서는...

class Car extends Vehicle{
  public void printspd(){
    System.out.println(speed);
  }
}
public subclass extends superclass

public subclass implements interface1, interface2

extends로 상속(1개만 상속 가능)했고, 2개 이상은 implements로 인터페이스를 통해서만 가능했고 명시적이다.

그리고 C#도 자바를 베낀지라... 상속은 1개만 가능하고, 2개 이상은 인터페이스를 통해서만 가능하다. 단, 사용법은 상속과 동일하게 : 만 찍어서 사용한다.

 

Program.cs

using System;

namespace MyApplication
{
  class Program
  { 
    static void Main(string[] args)
    {
      // Create a myCar object
      Car myCar = new Car();

      // Call the honk() method (From the Vehicle class) on the myCar object
      myCar.honk();

      // Display the value of the brand field (from the Vehicle class) and the value of the modelName from the Car class
      Console.WriteLine(myCar.brand + " " + myCar.modelName);
    }
  }
}

 

 

2. 한 눈에 안 들어오니 코드만 모아서 보면...

class Vehicle  // base class (parent) 
{
  public string brand = "Ford";  // Vehicle field
  public void honk()             // Vehicle method 
  {                    
    Console.WriteLine("Tuut, tuut!");
  }
}


class Car : Vehicle  // derived class (child)
{
  public string modelName = "Mustang";  // Car field
}


class Program
{
  static void Main(string[] args)
  {
    // Create a myCar object
    Car myCar = new Car();

    // Call the honk() method (From the Vehicle class) on the myCar object
    myCar.honk();

    // Display the value of the brand field (from the Vehicle class) and the value of the modelName from the Car class
    Console.WriteLine(myCar.brand + " " + myCar.modelName);
  }
}


결과 :
Tuut, tuut!
Ford Mustang

 

1. C#

class Person {
  private string name;	// field
  public string Name		// property
  {
    get { return name; }	// Getter
    set { name = value; }	// Setter
  }
}


class Main {
  static void Main(string[] args) {
    Person myObj = new Person();
    myObj.Name = "Anna";			// Set the value using 'Setter'
    Console.WriteLine(myObj.Name);	// Get the value using 'Getter'
  }
}

 

2. Java

public class Person {
  private String name;	// field

  // Getter
  public String getName() {
    return name;
  }

  // Setter
  public void setName(String newName) {
    this.name = newName;
  }
}


public class Main {
  public static void main(String[] args) {
    Person myObj = new Person();
    myObj.setName("Anna");			// Set the value using 'Setter'
    System.out.println(myObj.getName());	// Get the value using 'Getter'
  }
}

 

getter, setter야 IDE가 자동 생성 해주긴 하지만... 그래도 C#이 이 부분은 Java를 베껴 만들면서 최적화를 한 것 같다.

근데... 장점으로 볼 만 한건 코드는 짧고 간결해지는데... 단점으로는 역시 자바쪽이 훨씬 getter, setter가 명시적이라 가독성이 더 좋은 것 같다.

확실히 w3schools.com의 튜토리얼은 좋은 것 같다. 폴리텍 다니며 수업 듣기 이전에 파이썬, 자바, 자바스크립트 튜토리얼을 한 번 씩 먼저 봤다면 좋았을 것 같단 생각을 자주 하게 된다. 아무튼 별 생각 없이 작성하던 코딩을 이런식으로 체계적으로 정리하는 것은 정말 좋다 생각한다.

항상 클래스는 분리만 하거나 메인 메소드 안에 필드부터 다 선언하거나 그런식으로 난잡하게 짰는데 가독성이 훨씬 좋아 보인다.

 

클래스 내부의 필드나 메소드를 클래스 멤버라 한다

class Car {
  string model;
  string color;
  int year;

  static void Main(string[] args) {
    Car Ford = new Car();
    Ford.model = "Mustang";
    Ford.color = "red";
    Ford.year = 1969;

    Car Opel = new Car();
    Opel.model = "Astra";
    Opel.color = "white";
    Opel.year = 2005;

    Console.WriteLine(Ford.color + " " + Ford.year + " " + Ford.model);
    Console.WriteLine(Opel.color + " " + Opel.year + " " + Opel.model);
  }
}

1 ) 클래스 내부에 변수(fields)를 선언만 하고,
2 ) 메소드에서 클래스를 인스턴스(instance)화 하며 값을 넣으면
=> 간결하고 손 쉽게 여러 객체(objects)를 만들 수 있다.

 

잠깐... 근데 생성자를 쓰는게 더 좋아보이는 것 같은데...?

class Car {
  string model;
  string color;
  int year;
  
  public Car(string model, string color, int year) {
  	this.model = model;
    this.color = color;
    this.year = year;
  }

  static void Main(string[] args) {
    Car Ford = new Car("Mustang", "red", 1969);
    
    Car Opel = new Car("Astra", "white", 2005);

    Console.WriteLine(Ford.color + " " + Ford.year + " " + Ford.model);
    Console.WriteLine(Opel.color + " " + Opel.year + " " + Opel.model);
  }
}

뭐 어쨌든 중요한 개념은 '클래스 멤버 = 클래스 내부의 필드와 메소드'라는 것이다.

 

 

 

Reference : C# Class Members (Fields and Methods) (w3schools.com)

 

C# Class Members (Fields and Methods)

C# Class Members Class Members Fields and methods inside classes are often referred to as "Class Members": Example Create a Car class with three class members: two fields and one method. // The class class MyClass { // Class members   string color = "red"

www.w3schools.com

1. Method Overloading(메소드 오버로딩, 함수 오버로딩)

'과적'이라는 단어에 어울리게 함수 이름 하나로 다른 기능을 하는 함수를 만든다.

즉, 함수명이 같고, 함수 로직이 같다면 다른 것은 인풋 파라미터 뿐이다.

static int PlusMethod(int x, int y)
{
  return x + y;
}

static double PlusMethod(double x, double y)
{
  return x + y;
}

static void Main(string[] args)
{
  int myNum1 = PlusMethod(8, 5);
  double myNum2 = PlusMethod(4.3, 6.26);
  Console.WriteLine("Int: " + myNum1);
  Console.WriteLine("Double: " + myNum2);
}

-> 같은 함수명, 로직 & 다른 인풋 파라미터 타입 또는 개수

 

참고 : 같은 개념의 자바 코드

static int plusMethod(int x, int y) {
  return x + y;
}

static double plusMethod(double x, double y) {
  return x + y;
}

public static void main(String[] args) {
  int myNum1 = plusMethod(8, 5);
  double myNum2 = plusMethod(4.3, 6.26);
  System.out.println("int: " + myNum1);
  System.out.println("double: " + myNum2);
}

이래서 자바 짝퉁 소리 듣는거구나 싶다...

 

2. Method Overriding(메소드 오버라이딩, 함수 오버라이딩)

오버로딩과 헷갈릴 수 있는데... '최우선 되는'이라는 단어에 걸맞는 역할을 한다.

즉, 클래스를 상속받을 때, 상속받는 sub class(자식 클래스)에서 super class(부모 클래스)에 있는 '함수를 재정의'하는 것을 말한다. 이 때 재정의 된 메소드가 그 클래스 안에서 우선된다.

 

3. Polymorphism(다형성) 자바와의 차이

Java

class Animal {
  public void animalSound() {
    System.out.println("The animal makes a sound");
  }
}

class Pig extends Animal {
  public void animalSound() {
    System.out.println("The pig says: wee wee");
  }
}

class Dog extends Animal {
  public void animalSound() {
    System.out.println("The dog says: bow wow");
  }
}

class Main {
  public static void main(String[] args) {
    Animal myAnimal = new Animal();  // Create a Animal object
    Animal myPig = new Pig();  // Create a Pig object
    Animal myDog = new Dog();  // Create a Dog object
    myAnimal.animalSound();
    myPig.animalSound();
    myDog.animalSound();
  }
}


결과 :
The animal makes a sound
The pig says: wee wee
The dog says: bow wow

@Override annotation을 명시하지 않아도 기본적으로 method overriding이 된다.


C#

class Animal  // Base class (parent) 
{
  public void animalSound() 
  {
    Console.WriteLine("The animal makes a sound");
  }
}

class Pig : Animal  // Derived class (child) 
{
  public void animalSound() 
  {
    Console.WriteLine("The pig says: wee wee");
  }
}

class Dog : Animal  // Derived class (child) 
{
  public void animalSound() 
  {
    Console.WriteLine("The dog says: bow wow");
  }
}

class Program 
{
  static void Main(string[] args) 
  {
    Animal myAnimal = new Animal();  // Create a Animal object
    Animal myPig = new Pig();  // Create a Pig object
    Animal myDog = new Dog();  // Create a Dog object

    myAnimal.animalSound();
    myPig.animalSound();
    myDog.animalSound();
  }
}


결과 :
The animal makes a sound
The animal makes a sound
The animal makes a sound

기본적으로 Overriding이 되지 않는다.

 

class Animal  // Base class (parent) 
{
  public virtual void animalSound() 
  {
    Console.WriteLine("The animal makes a sound");
  }
}

class Pig : Animal  // Derived class (child) 
{
  public override void animalSound() 
  {
    Console.WriteLine("The pig says: wee wee");
  }
}

class Dog : Animal  // Derived class (child) 
{
  public override void animalSound() 
  {
    Console.WriteLine("The dog says: bow wow");
  }
}

class Program 
{
  static void Main(string[] args) 
  {
    Animal myAnimal = new Animal();  // Create a Animal object
    Animal myPig = new Pig();  // Create a Pig object
    Animal myDog = new Dog();  // Create a Dog object

    myAnimal.animalSound();
    myPig.animalSound();
    myDog.animalSound();
  }
}


결과 :
The animal makes a sound
The pig says: wee wee
The dog says: bow wow

override를 명시 해야지만 작동한다. 자바에서처럼 annotation으로 하지 않고 public override void 이런식으로 메소드 정의할 때 앞에 쓰는구나..

 

Tag. java overloading, java overriding, 자바 오버로딩, 자바 오버라이딩, 어노테이션, annotation, @override, @Override, 자바 다형성, 자바 polymorphism, java polymorphism, 자바 polymorphism, c# polymorphism, c sharp polymorphsim, 씨샵 polymorphism, c# 다형성, c sharp 다형성, 씨샵 다형성, 자바와 비교

static void MyMethod(string country = "Norway") 
{
  Console.WriteLine(country);
}

static void Main(string[] args)
{
  MyMethod("Sweden");
  MyMethod("India");
  MyMethod();
  MyMethod("USA");
}

// Sweden
// India
// Norway
// USA

인풋 파라미터가 없을 때는 메소드에 선언한 기본 파라미터가 대입된다.

 

Tag. 함수 기본 파라미터, 메소드 기본 파라미터, function default parameter, method default parameter

+ Recent posts