More Related Content
More from RaynaITSTEP (20)
.NET/C#_10
- 5. Интерфейси
public class Person : IShowable {
protected string name;
protected int age;
public Person(string name, int age) {
this.name = name;
this.age = age;
}
public void show() {
Console.WriteLine(name + “ ” + age);
}}
- 6. Интерфейси
public class Book : IShowable {
protected string title;
protected decimal price;
public Book(String title, decimal price) {
this.title = title;
this.price = price;
}
public void show() {
Console.WriteLine(title + “ ” + price);
}}
- 7. Интерфейси - използване
IShowable p1 = new Person(“Jill”, 23);
IShowable b1 = new Book(“Book Title”, 7);
IShowable[] thingsToShow = {p1,b1};
foreach(IShowable s in thingsToShow) s.show();
- 9. Наследяване на интерфейси
public class Pixel : IShowableAndDrawable {
protected int r, g, b;
public Person(int r, int g, int b) {
this.r =r;
this.g = g;
this.b = b;
}
public void show() {
Console.WriteLine(r + “ ” + g + “ ” + b);
}
public void draw() {
Console.WriteLine(“drawing ” + r + “ ” + g + “ ” + b);
}}