Slide
Lý thuyết

Tính trừu tượng (abstraction): Đây là khả năng của chương trình bỏ qua hay không chú ý đến một số khía cạnh của thông tin mà nó đang trực tiếp làm việc lên, nghĩa là nó có khả năng tập trung vào những cốt lõi cần thiết. Mỗi đối tượng phục vụ như là một “động tử” có thể hoàn tất các công việc một cách nội bộ, báo cáo, thay đổi trạng thái của nó và liên lạc với các đối tượng khác mà không cần cho biết làm cách nào đối tượng tiến hành được các thao tác. Tính chất này thường được gọi là sự trừu tượng của dữ liệu.
Tính trừu tượng còn thể hiện qua việc một đối tượng ban đầu có thể có một số đặc điểm chung cho nhiều đối tượng khác như là sự mở rộng của nó nhưng bản thân đối tượng ban đầu này có thể không có các biện pháp thi hành. Tính trừu tượng này thường được xác định trong khái niệm gọi là lớp trừu tượng hay lớp cơ sở trừu tượng.
// Abstract class
abstract class Animal
{
// Abstract method (does not have a body)
public abstract void animalSound();
// Regular method
public void sleep()
{
Console.WriteLine("Zzz");
}
}
// Derived 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
}
}
}
* Chú ý: Animal myObj = new Animal(); sẽ bị lỗi
Một ví dụ khác về hình dạng sự vật
namespace DemoAbstraction
{
// abstract class
abstract class Shape
{
// abstract method
// No direct access
public abstract int area();
}
// square class inheriting
// the Shape class
class Square : Shape
{
// private data member
private int side;
// method of square class
public Square(int x = 0)
{
side = x;
}
// overriding of the abstract method of Shape
// class using the override keyword
public override int area()
{
Console.Write("Area of Square: ");
return (side * side);
}
}
class Geeks
{
static void Main(string[] args)
{
// creating reference of Shape class
// which refer to Square class instance
Shape sh = new Square(4);
// calling the method
double result = sh.area();
Console.Write("{0}", result);
}
}
}
Bài tập
Bài tập 1

Kiến thức:
- Không thể viết Shape A = new Shape() bởi vì Shape là Abstract class (lớp trừu tượng), không thể tạo ra đối tượng (object).
- Đối với các phương thức trừu tượng (abstract method) như là public abstract double getPerimeter() và public abstract double getArea() thì phải thêm từ abstract khi định nghĩa, không có phần mô tả phương thức (không có mở ngoặc nhọn rồi viết code vào). Bắt buộc lớp kế thừa phải định nghĩa phương thức đó lại, và có mô tả phương thức làm gì gì….
- Đối với các phương thức như virtual toString() không phải là phương thức trừu tượng thì có mô tả phương thức trong đó có các dòng code mô tả thân phương thức. Ở lớp kế thừa không bắt buộc định nghĩa lại. Nếu định nghĩa lại thì có thêm từ khóa override.
public abstract class Shape
{
// Attributes
protected string color;
protected bool filled;
// Constructors
public Shape() { }
public Shape(string color, bool filled)
{
this.color = color;
this.filled = filled;
}
//Method thường
public String getColor()
{
return color;
}
public void setColor(String color)
{
this.color = color;
}
public bool isFilled()
{
return filled;
}
public void setFilled(bool filled)
{
this.filled = filled;
}
// Methods abstract
public abstract double getPerimeter();
public abstract double getArea();
//method virtual
public virtual String toString()
{
return (this.color + " " + this.filled);
}
}
class Rectangle:Shape
{
protected double width;
protected double length;
public Rectangle() { }
public Rectangle(double Width, double Length) // nếu dài hoặc rộng <=0 thì thiết lập về 1, nếu >0 thì bình thường
{
if (Width > 0)
width = Width;
else
{
width = 1;
Console.WriteLine("Chieu rong <=0 khong phu hop nen set ve 1"); } if (Length > 0)
length = Length;
else
{
length = 1;
Console.WriteLine("Chieu dai <=0 khong phu hop nen set ve 1");
}
}
public double getWidth()
{
return this.width;
}
public void setWidth(double width)
{
this.width = width;
}
public double getLength()
{
return this.length;
}
public void setLength(double length)
{
this.length = length;
}
public override double getArea()
{
return (width*length);
}
public override double getPerimeter()
{
return (2 * (width+length));
}
public override String toString()
{
return ("Rectangle" + " " + this.color + " " + this.filled + " " + width + " " + length);
}
}
class Square:Rectangle
{
protected double side;
public Square()
{
}
public Square(double side)
{
this.side = side;
}
public double getSide()
{
return this.side;
}
public void setSide(double side)
{
this.side = side;
}
//abstract method
public override double getArea()
{
return (side*side);
}
public override double getPerimeter()
{
return (side * 4);
}
public override String toString()
{
return ("Square" + " " + this.color + " " + this.filled + " " + side);
}
}
class Circle:Shape
{
protected double radius;
public Circle() { }
public Circle(double radius)
{
this.radius = radius;
}
public Circle(double radius, String color, bool filled):base(color,filled)
{
this.radius = radius;
}
public double getRadius()
{
return this.radius;
}
public void setRadius(double radius)
{
this.radius = radius;
}
//abstract method
public override double getArea()
{
return (radius * radius * Math.PI);
}
public override double getPerimeter()
{
return (2 * radius * Math.PI);
}
public override String toString()
{
return ("Circle" + " " + this.color + " " + this.filled + " " + radius);
}
}
Bài tập 2
Hãy định nghĩa các phương thức Add, Subtract, Multiply, Divive ở lớp Fraction thành phương thức tĩnh (static method)
class Fraction
{
protected int numerator;
protected int denominator;
//Property
public int Numerator
{
get { return numerator; }
set { numerator = value; }
}
public int Denominator
{
get { return denominator; }
set { denominator = value; }
}
public Fraction()
{
this.numerator = 0;
this.denominator = 1;
}
public Fraction(int numerator, int denominator)
{
this.numerator = numerator;
this.denominator = denominator;
Simplify();
}
public void Simplify()
{
int uocsochunglonnhat = USCLN(this.numerator, this.denominator);
this.numerator /= uocsochunglonnhat;
this.denominator /= uocsochunglonnhat;
}
static int USCLN(int a, int b)
{
if (b == 0) return a;
return USCLN(b, a % b);
}
public Fraction(int numerator)
{
this.numerator = numerator;
this.denominator = 1;
}
public void Input()
{
Console.Write("Input numerator (Nhập tử số) : ");
this.numerator = Int32.Parse(Console.ReadLine());
int x;
do
{
Console.Write("Inputdenominator (Nhập mẫu số) khác không 0: ");
x = Int32.Parse(Console.ReadLine());
}
while (x == 0);
Simplify();
}
public double Decimal()
{
return (this.numerator / this.denominator);
}
public static Fraction Add(Fraction a, Fraction b)
{
Fraction c = new Fraction();
c.numerator = a.numerator * b.denominator + a.denominator * b.numerator;
c.denominator = a.denominator * b.denominator;
c.Simplify();
return c;
}
public static Fraction Subtract(Fraction a, Fraction b)
{
Fraction c = new Fraction();
c.numerator = a.numerator * b.denominator + a.denominator * b.numerator;
c.denominator = a.denominator * b.denominator;
c.Simplify();
return c;
}
public static Fraction Multiply(Fraction a, Fraction b)
{
Fraction c = new Fraction();
c.numerator = a.numerator * b.numerator;
c.denominator = a.denominator * b.denominator;
c.Simplify();
return c;
}
public static Fraction Divide(Fraction a, Fraction b)
{
Fraction c = new Fraction();
c.numerator = a.numerator * b.denominator;
c.denominator = a.denominator * b.numerator;
c.Simplify();
return c;
}
public virtual string ToString()
{
return (this.numerator + "/" + this.denominator);
}
public static Fraction operator -(Fraction a)
{
Fraction c = new Fraction();
c.numerator = -a.numerator;
c.denominator = a.denominator;
return c;
}
public static Fraction operator +(Fraction a, Fraction b)
{
Fraction c = new Fraction();
c.numerator = a.numerator * b.denominator + a.denominator * b.numerator;
c.denominator = a.denominator * b.denominator;
c.Simplify();
return c;
}
public static Fraction operator -(Fraction a, Fraction b)
{
Fraction c = new Fraction();
c.numerator = a.numerator * b.denominator + a.denominator * b.numerator;
c.denominator = a.denominator * b.denominator;
c.Simplify();
return c;
}
public static Fraction operator *(Fraction a, Fraction b)
{
Fraction c = new Fraction();
c.numerator = a.numerator * b.numerator;
c.denominator = a.denominator * b.denominator;
c.Simplify();
return c;
}
public static Fraction operator /(Fraction a, Fraction b)
{
Fraction c = new Fraction();
c.numerator = a.numerator * b.denominator;
c.denominator = a.denominator * b.numerator;
c.Simplify();
return c;
}
public static bool operator ==(Fraction a, Fraction b)
{
if (a.numerator == b.numerator && a.denominator == b.denominator)
return true;
else
return false;
}
public static bool operator !=(Fraction a, Fraction b)
{
if (a.numerator != b.numerator || a.denominator != b.denominator)
return true;
else
return false;
}
public static bool operator >(Fraction a, Fraction b)
{
Fraction a1 = new Fraction();
Fraction b1 = new Fraction();
a1.numerator = a.numerator * b.denominator;
a1.denominator = a.denominator * b.denominator;
b1.numerator = b.numerator * a.denominator;
b1.denominator = b.denominator * a.denominator;
if (a1.numerator > b1.numerator)
return true;
else
return false;
}
public static bool operator <(Fraction a, Fraction b)
{
Fraction a1 = new Fraction();
Fraction b1 = new Fraction();
a1.numerator = a.numerator * b.denominator;
a1.denominator = a.denominator * b.denominator;
b1.numerator = b.numerator * a.denominator;
b1.denominator = b.denominator * a.denominator;
if (a1.numerator < b1.numerator) return true; else return false; } public static bool operator >=(Fraction a, Fraction b)
{
Fraction a1 = new Fraction();
Fraction b1 = new Fraction();
a1.numerator = a.numerator * b.denominator;
a1.denominator = a.denominator * b.denominator;
b1.numerator = b.numerator * a.denominator;
b1.denominator = b.denominator * a.denominator;
if (a1.numerator > b1.numerator)
return true;
else
return false;
}
public static bool operator <=(Fraction a, Fraction b)
{
Fraction a1 = new Fraction();
Fraction b1 = new Fraction();
a1.numerator = a.numerator * b.denominator;
a1.denominator = a.denominator * b.denominator;
b1.numerator = b.numerator * a.denominator;
b1.denominator = b.denominator * a.denominator;
if (a1.numerator < b1.numerator)
return true;
else
return false;
}
}