Abstraktio
Abstraktio on olio-ohjelmoinnin (OOP) keskeinen käsite, jonka avulla voidaan piilottaa monimutkaiset toteutustiedot ja keskittyä olennaisiin toiminnallisuuksiin. C#:ssa abstraktio toteutetaan abstraktien luokkien avulla.
OOP perustui alun perin kolmeen periaatteeseen (joita kutsutaan joskus nimellä "OOP:n kolme pilaria"), jotka ovat kapselointi, perintä ja polymorfismi. Näin ollen abstraktio on uusi lisäys, eikä sitä aina pidetä perustavanlaatuisena käsitteenä joidenkin lähteiden mukaan, mutta se on kuitenkin tärkeä käsite.
Abstrakti luokka on luokka, jota ei voi instantioida, eli siitä ei voi luoda oliota. Abstrakti luokka voi sisältää ominaisuuksia ja metodeja kuten mikä tahansa muu luokka, mutta se voi sisältää myös abstrakteja metodeja, jotka ovat mallimetodeja ja tarkoitettu johdettujen luokkien toteutettaviksi.
Abstrakti luokka luodaan lisäämällä abstract
-avainsana luokan määrittelyn eteen. Esimerkiksi luodaan abstrakti luokka nimeltä Shape
:
index.cs
12345678910111213141516171819using System; abstract class Shape { protected float circumference; public float getCircumference() { return circumference; } } class ConsoleApp { static void Main() { Shape s = new Shape(); // Error: Cannot create an instance of an abstract class } }
Vastaavasti voit luoda abstraktin metodin lisäämällä avainsanan abstract
palautustyypin eteen. Abstraktilla metodilla ei ole runkoa – se toimii ainoastaan mallipohjana:
index.cs
1234567891011abstract class Shape { protected float circumference; public float getCircumference() { return circumference; } public abstract float getArea(); }
Tämän tarkoituksena on luoda mallipohja muille luokille. Tämä auttaa yksinkertaistamaan koodia. Ymmärtääksemme tämän paremmin, tarkastellaan Polymorphism-tehtävää luvusta 5:
index.cs
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102using System; class Shape { // Perimeter is the length of the 'outline' of a shape protected float perimeter; public float getPerimeter() { return perimeter; } public virtual float getArea() { return 0.0f; } // A virtual method can only be either 'public' or 'protected' protected virtual float calculatorPerimeter() { return 0.0f; } } class Rectangle : Shape { float width; float height; public Rectangle(float width, float height) { this.width = width; this.height = height; this.perimeter = getPerimeter(); } public override float getArea() { return width * height; } protected override float calculatorPerimeter() { return width * 2 + height * 2; } } class Square : Shape { float length; public Square(float length) { this.length = length * length; } public override float getArea() { return length * length; } protected override float calculatorPerimeter() { return 4 * length; } } class Circle : Shape { float radius; public Circle(float radius) { this.radius = radius; } // Area of a Circle = pi . r . r public override float getArea() { return 3.14f * radius * radius; } // Perimeter (or) Circumference: 2 . pi . r protected override float calculatorPerimeter() { return 2.00f * 3.14f * radius; } } class ConsoleApp { static void Main() { Rectangle r1 = new Rectangle(10f, 20f); Square s1 = new Square(10f); Circle c1 = new Circle(10f); Console.WriteLine(r1.getArea()); Console.WriteLine(s1.getArea()); Console.WriteLine(c1.getArea()); } }
Yllä olevassa esimerkissä et koskaan aio käyttää Shape
-luokkaa, mutta sinun täytyi silti kirjoittaa näennäistoteutukset getArea
- ja calculatePerimeter
-metodeille Shape
-luokan sisälle. Voit yksinkertaistaa tätä koodia tekemällä Shape
-luokasta abstraktin, ja lisäksi voit tehdä myös getArea
- ja calculatePerimeter
-metodeista abstrakteja.
Metodi voi olla joko abstrakti tai virtuaalinen, mutta ei molempia samanaikaisesti. On kuitenkin tärkeää huomata, että abstrakti metodi on pohjimmiltaan virtuaalinen (ylikirjoitettava) metodi, mutta sillä ei ole runkoa kantaluokassa.
index.cs
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100using System; using System.Collections.Generic; abstract class Shape { // Perimeter is the length of the 'outline' of a shape protected float perimeter; public float getPerimeter() { return perimeter; } public abstract float getArea(); protected abstract float calculatorPerimeter(); } class Rectangle : Shape { float width; float height; public Rectangle(float width, float height) { this.width = width; this.height = height; this.perimeter = getPerimeter(); } public override float getArea() { return width * height; } protected override float calculatorPerimeter() { return width * 2 + height * 2; } } class Square : Shape { float length; public Square(float length) { this.length = length * length; } public override float getArea() { return length * length; } protected override float calculatorPerimeter() { return 4 * length; } } class Circle : Shape { float radius; public Circle(float radius) { this.radius = radius; } public override float getArea() { return 3.14f * radius * radius; } protected override float calculatorPerimeter() { return 2.00f * 3.14f * radius; } } class ConsoleApp { static void Main() { Rectangle r1 = new Rectangle(10f, 20f); Square s1 = new Square(10f); Circle c1 = new Circle(10f); // We cannot create an instance of 'Shape' but we can use type datatype "Shape" for creating variables, arrays or lists List<Shape> shapes = new List<Shape>(); shapes.Add(r1); shapes.Add(s1); shapes.Add(c1); foreach(Shape shape in shapes) { Console.WriteLine(shape.getArea()); } } }
1. Mitä avainsanaa käytetään tekemään luokasta abstrakti?
2. Voiko abstraktia luokkaa olioistaa?
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Can you show me an example of an abstract class with abstract methods in C#?
What is the difference between an abstract class and an interface in C#?
How do derived classes implement abstract methods?
Awesome!
Completion rate improved to 2.04
Abstraktio
Pyyhkäise näyttääksesi valikon
Abstraktio on olio-ohjelmoinnin (OOP) keskeinen käsite, jonka avulla voidaan piilottaa monimutkaiset toteutustiedot ja keskittyä olennaisiin toiminnallisuuksiin. C#:ssa abstraktio toteutetaan abstraktien luokkien avulla.
OOP perustui alun perin kolmeen periaatteeseen (joita kutsutaan joskus nimellä "OOP:n kolme pilaria"), jotka ovat kapselointi, perintä ja polymorfismi. Näin ollen abstraktio on uusi lisäys, eikä sitä aina pidetä perustavanlaatuisena käsitteenä joidenkin lähteiden mukaan, mutta se on kuitenkin tärkeä käsite.
Abstrakti luokka on luokka, jota ei voi instantioida, eli siitä ei voi luoda oliota. Abstrakti luokka voi sisältää ominaisuuksia ja metodeja kuten mikä tahansa muu luokka, mutta se voi sisältää myös abstrakteja metodeja, jotka ovat mallimetodeja ja tarkoitettu johdettujen luokkien toteutettaviksi.
Abstrakti luokka luodaan lisäämällä abstract
-avainsana luokan määrittelyn eteen. Esimerkiksi luodaan abstrakti luokka nimeltä Shape
:
index.cs
12345678910111213141516171819using System; abstract class Shape { protected float circumference; public float getCircumference() { return circumference; } } class ConsoleApp { static void Main() { Shape s = new Shape(); // Error: Cannot create an instance of an abstract class } }
Vastaavasti voit luoda abstraktin metodin lisäämällä avainsanan abstract
palautustyypin eteen. Abstraktilla metodilla ei ole runkoa – se toimii ainoastaan mallipohjana:
index.cs
1234567891011abstract class Shape { protected float circumference; public float getCircumference() { return circumference; } public abstract float getArea(); }
Tämän tarkoituksena on luoda mallipohja muille luokille. Tämä auttaa yksinkertaistamaan koodia. Ymmärtääksemme tämän paremmin, tarkastellaan Polymorphism-tehtävää luvusta 5:
index.cs
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102using System; class Shape { // Perimeter is the length of the 'outline' of a shape protected float perimeter; public float getPerimeter() { return perimeter; } public virtual float getArea() { return 0.0f; } // A virtual method can only be either 'public' or 'protected' protected virtual float calculatorPerimeter() { return 0.0f; } } class Rectangle : Shape { float width; float height; public Rectangle(float width, float height) { this.width = width; this.height = height; this.perimeter = getPerimeter(); } public override float getArea() { return width * height; } protected override float calculatorPerimeter() { return width * 2 + height * 2; } } class Square : Shape { float length; public Square(float length) { this.length = length * length; } public override float getArea() { return length * length; } protected override float calculatorPerimeter() { return 4 * length; } } class Circle : Shape { float radius; public Circle(float radius) { this.radius = radius; } // Area of a Circle = pi . r . r public override float getArea() { return 3.14f * radius * radius; } // Perimeter (or) Circumference: 2 . pi . r protected override float calculatorPerimeter() { return 2.00f * 3.14f * radius; } } class ConsoleApp { static void Main() { Rectangle r1 = new Rectangle(10f, 20f); Square s1 = new Square(10f); Circle c1 = new Circle(10f); Console.WriteLine(r1.getArea()); Console.WriteLine(s1.getArea()); Console.WriteLine(c1.getArea()); } }
Yllä olevassa esimerkissä et koskaan aio käyttää Shape
-luokkaa, mutta sinun täytyi silti kirjoittaa näennäistoteutukset getArea
- ja calculatePerimeter
-metodeille Shape
-luokan sisälle. Voit yksinkertaistaa tätä koodia tekemällä Shape
-luokasta abstraktin, ja lisäksi voit tehdä myös getArea
- ja calculatePerimeter
-metodeista abstrakteja.
Metodi voi olla joko abstrakti tai virtuaalinen, mutta ei molempia samanaikaisesti. On kuitenkin tärkeää huomata, että abstrakti metodi on pohjimmiltaan virtuaalinen (ylikirjoitettava) metodi, mutta sillä ei ole runkoa kantaluokassa.
index.cs
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100using System; using System.Collections.Generic; abstract class Shape { // Perimeter is the length of the 'outline' of a shape protected float perimeter; public float getPerimeter() { return perimeter; } public abstract float getArea(); protected abstract float calculatorPerimeter(); } class Rectangle : Shape { float width; float height; public Rectangle(float width, float height) { this.width = width; this.height = height; this.perimeter = getPerimeter(); } public override float getArea() { return width * height; } protected override float calculatorPerimeter() { return width * 2 + height * 2; } } class Square : Shape { float length; public Square(float length) { this.length = length * length; } public override float getArea() { return length * length; } protected override float calculatorPerimeter() { return 4 * length; } } class Circle : Shape { float radius; public Circle(float radius) { this.radius = radius; } public override float getArea() { return 3.14f * radius * radius; } protected override float calculatorPerimeter() { return 2.00f * 3.14f * radius; } } class ConsoleApp { static void Main() { Rectangle r1 = new Rectangle(10f, 20f); Square s1 = new Square(10f); Circle c1 = new Circle(10f); // We cannot create an instance of 'Shape' but we can use type datatype "Shape" for creating variables, arrays or lists List<Shape> shapes = new List<Shape>(); shapes.Add(r1); shapes.Add(s1); shapes.Add(c1); foreach(Shape shape in shapes) { Console.WriteLine(shape.getArea()); } } }
1. Mitä avainsanaa käytetään tekemään luokasta abstrakti?
2. Voiko abstraktia luokkaa olioistaa?
Kiitos palautteestasi!