Examination & Test - Lập trình C# - Lập trình C Sharp
1. Tạo 1 lớp giao diện IInfor chứa hàm showInfor
2. Tạo lớp đối tượng People gồm các thuộc tính tên, tuổi, địa chỉ kế thừa từ lớp IInfor
- Tạo hàm input
Tạo lớp Car gồm các thuộc tính tên và màu -> kế thừa từ lớp IInfor
- Tạo hàm input
Viết code cho các lớp trên.
3. Trọng phương thức main của lớp Test tạo 2 đối tượng People và Car.
- tạo phương thức như sau.
public static void showInfor(List<IInfor> a) -> hàm này hiển thị thông tin tất cả đối tượng.
Viết chương trình và sử dụng hàm showInfor trong phương thức main.
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Triệu Văn Lăng [T2008A]](https://www.gravatar.com/avatar/1348e3562c6492c26f796cb1f45982a1.jpg?s=80&d=mm&r=g)
Triệu Văn Lăng
2021-05-28 10:14:04
#Car.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace bai1505
{
class Car : IInfor
{
public string name { get; set; }
public string color { get; set; }
public Car()
{
}
public Car(string name, string color)
{
this.name = name;
this.color = color;
}
public void input()
{
Console.WriteLine("Nhap thong tin xe:");
Console.WriteLine("Nhap ten xe:");
name = Console.ReadLine();
Console.WriteLine("Nhap mau xe:");
color = Console.ReadLine();
}
public void showInfor()
{
Console.WriteLine("Ten: {0}, Mau: {1}", name, color);
}
}
}
#IInfor.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace bai1505
{
interface IInfor
{
public void showInfor()
{
}
}
}
#People.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace bai1505
{
class People : IInfor
{
public string name { get; set; }
public int age { get; set; }
public string address { get; set; }
public People()
{
}
public People(string name, int age, string address)
{
this.name = name;
this.age = age;
this.address = address;
}
public void input()
{
Console.WriteLine("Nhap thong tin nguoi:");
Console.WriteLine("Nhap ten:");
name = Console.ReadLine();
Console.WriteLine("Nhap tuoi:");
age = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Nhap dia chi:");
address = Console.ReadLine();
}
public void showInfor()
{
Console.WriteLine("Ten: {0}, Tuoi: {1}, Dia chi: {2}", name, age, address);
}
}
}
#Program.cs
using System;
using bai1505;
using System.Collections.Generic;
namespace bai1505
{
class Program
{
static Car car = new Car();
static People people = new People();
static List<IInfor> inforList = new List<IInfor>();
static void Main(string[] args)
{
people.input();
car.input();
inforList.Add(people);
inforList.Add(car);
showInfor(inforList);
}
public static void showInfor(List<IInfor> a)
{
foreach (var item in a)
{
item.showInfor();
}
}
}
}
![Nguyễn Tiến Đạt [T2008A]](https://www.gravatar.com/avatar/b5819cd0adc95c727c7ad0c2bcf6098b.jpg?s=80&d=mm&r=g)
Nguyễn Tiến Đạt
2021-05-28 09:30:32
#Car.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace People_And_Car
{
class Car : IInfor
{
public string name { get; set; }
public string color { get; set; }
public Car()
{
}
public void input()
{
Console.WriteLine("Ten:");
name = Console.ReadLine();
Console.WriteLine("Mau sac:");
color = Console.ReadLine();
}
public void showInfor()
{
Console.WriteLine("Ten: {0}, Mau sac: {1}", name, color);
}
}
}
#IInfor.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace People_And_Car
{
interface IInfor
{
public void showInfor();
}
}
#People.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace People_And_Car
{
class People : IInfor
{
public string name { get; set; }
public int age { get; set; }
public string address { get; set; }
public People()
{
}
public void input()
{
Console.WriteLine("Ten:");
name = Console.ReadLine();
Console.WriteLine("Tuoi:");
age = int.Parse(Console.ReadLine());
Console.WriteLine("Dia chi:");
address = Console.ReadLine();
}
public void showInfor()
{
Console.WriteLine("Ten: {0}, Tuoi: {1}, Dia chi: {2}", name, age, address);
}
}
}
#Program.cs
using System;
using System.Collections.Generic;
using People_And_Car;
namespace People_And_Car
{
class Program
{
static void Main(string[] args)
{
People people = new People();
people.input();
Car car = new Car();
car.input();
List<IInfor> list = new List<IInfor>();
list.Add(people);
list.Add(car);
showInfor(list);
}
public static void showInfor(List<IInfor> a)
{
foreach (var item in a)
{
item.showInfor();
}
}
}
}
![hainguyen [T2008A]](https://www.gravatar.com/avatar/32855ce6db55d60134d830aee06b41e5.jpg?s=80&d=mm&r=g)
hainguyen
2021-05-28 09:04:23
#Car.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Lesson07
{
class Car : IInfor
{
public string Name { get; set; }
public string Color { get; set; }
public Car() { }
public void Input()
{
Console.WriteLine("Nhap Name: ");
Name = Console.ReadLine();
Console.WriteLine("Nhap Color: ");
Color = Console.ReadLine();
}
public override void showInfor()
{
Console.WriteLine("Name: = {0}, Color: = {1}", Name, Color);
}
}
}
#IInfor.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Lesson07
{
abstract class IInfor
{
public abstract void showInfor();
}
}
#People.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Lesson07
{
class People : IInfor
{
public string Ten { get; set; }
public int Tuoi { get; set; }
public string DiaChi { get; set; }
public People() { }
public void Input()
{
Console.WriteLine("Nhap ten: ");
Ten = Console.ReadLine();
Console.WriteLine("Nhap Tuoi: ");
Tuoi = int.Parse(Console.ReadLine());
Console.WriteLine("Nhap DiaChi: ");
DiaChi = Console.ReadLine();
}
public override void showInfor()
{
Console.WriteLine("Ten: = {0}, Tuoi: = {1}, DiaChi: = {2}", Ten, Tuoi, DiaChi);
}
}
}
#Program.cs
using System;
using System.Collections.Generic;
namespace Lesson07
{
class Program
{
public static void showInfor(List<IInfor> a)
{
foreach(IInfor i in a)
{
i.showInfor();
}
}
static void Main(string[] args)
{
List<IInfor> inforlist = new List<IInfor>();
People people = new People();
people.Input();
inforlist.Add(people);
Car car = new Car();
car.Input();
inforlist.Add(car);
}
}
}
![Trần Văn Lâm [T2008A]](https://www.gravatar.com/avatar/cfc15c8cb7781ad669b013e01f9f1a6b.jpg?s=80&d=mm&r=g)
Trần Văn Lâm
2021-05-28 08:03:43
#Car.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace les7bt1
{
class Car : IInfor
{
public string Name { get; set; }
public string Color { get; set; }
public Car()
{
}
public Car(string Name, string Color)
{
this.Name = Name;
this.Color = Color;
}
public void Input()
{
Console.WriteLine("Nhap Ten Xe:");
Name = Console.ReadLine();
Console.WriteLine("Nhap Mau:");
Color = Console.ReadLine();
}
public override void showInfor()
{
Console.WriteLine("Ten Xe : {0}, Mau Xe : {1}");
}
}
}
#IInfor.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace les7bt1
{
abstract class IInfor
{
public abstract void showInfor();
}
}
#People.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace les7bt1
{
class People : IInfor
{
public string Name { get; set; }
public int Age { get; set; }
public string Address { get; set; }
public People()
{
}
public People(string Name, int Age, string Address)
{
this.Name = Name;
this.Age = Age;
this.Address = Address;
}
public void Input()
{
Console.WriteLine("Nhap Ten:");
Name = Console.ReadLine();
Console.WriteLine("Nhap Tuoi:");
Age = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Nhap Dia Chi:");
Address = Console.ReadLine();
}
public override void showInfor()
{
Console.WriteLine("Ten : {0}, Tuoi : {1}, Dia Chi: {2}", Name, Age, Address);
}
}
}
#Test.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace les7bt1
{
class Test
{
static void Main(string[] args)
{
List<IInfor> inforList = new List<IInfor>();
People people = new People();
people.Input();
inforList.Add(people);
Car car = new Car();
car.Input();
inforList.Add(car);
showInfor(inforList);
}
public static void showInfor(List<IInfor> a)
{
foreach (IInfor i4 in a)
{
i4.showInfor();
}
}
}
}
![Nguyễn Hoàng Anh [C1907L]](https://www.gravatar.com/avatar/5b7bb435cae0d0a0fd414f6fdd0adc87.jpg?s=80&d=mm&r=g)
Nguyễn Hoàng Anh
2020-07-12 14:50:21
using System;
using System.Collections.Generic;
namespace Interface
{
class Program
{
public static void showInfor(List<IInfor> a)
{
for (int i = 0; i < a.Count; i++) {
a[i].showInfor();
}
}
static void Main(string[] args)
{
List<IInfor> a = new List<IInfor>();
People p = new People();
Car c = new Car();
p.input();
c.input();
a.Add(p);
a.Add(c);
showInfor(a);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Interface
{
interface IInfor
{
public void showInfor();
}
}
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
namespace Interface
{
class People : IInfor
{
public int Age { get; set; }
public string Address { get; set; }
public string Name { get; set; }
public void input(){
Console.WriteLine("Enter person name : ");
Name = Console.ReadLine();
Console.WriteLine("Enter person age : ");
Age = int.Parse(Console.ReadLine());
Console.WriteLine("Enter person address : ");
Address = Console.ReadLine();
}
public void showInfor() {
Console.WriteLine("Person name : {0} , age : {1} , address : {2}",Name,Age,Address);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Interface
{
class Car : IInfor
{
public string Name { get; set; }
public string Color { get; set; }
public void showInfor() {
Console.WriteLine("Car name : {0} , color : {1}.", Name, Color);
}
public void input() {
Console.WriteLine("Enter car name : ");
Name = Console.ReadLine();
Console.WriteLine("Enter car color : ");
Color = Console.ReadLine();
}
}
}
![Ngô Quang Huy [C1907L]](https://www.gravatar.com/avatar/8e54dbf5994077ce599f49278164ae79.jpg?s=80&d=mm&r=g)
Ngô Quang Huy
2020-07-11 08:57:34
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
List<IInfor> tmp = new List<IInfor>();
People pp = new People();
Car car = new Car();
pp.input();
car.input();
tmp.Add(pp);
tmp.Add(car);
showInfor(tmp);
}
public static void showInfor(List<IInfor> a)
{
foreach (IInfor infor in a)
{
infor.showInfor();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
interface IInfor
{
void showInfor();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class People : IInfor
{
public string Name { get; set; }
public int Age{ get; set; }
public string Address{ get; set; }
public People()
{
}
public void showInfor()
{
Console.WriteLine("Name = {0}, Age = {1}, Address = {2}",Name,Age,Address);
}
public void input()
{
Console.WriteLine("Insert Name: ");
Name = Console.ReadLine();
Console.WriteLine("Insert Age: ");
Age = int.Parse(Console.ReadLine());
Console.WriteLine("Insert Address: ");
Address = Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Car : IInfor
{
public string Name { get; set; }
public string Color { get; set; }
public Car()
{
}
public void showInfor()
{
Console.WriteLine("Name = {0}, Color = {1}", Name, Color);
}
public void input()
{
Console.WriteLine("Insert Name: ");
Name = Console.ReadLine();
Console.WriteLine("Insert Color: ");
Color = Console.ReadLine();
}
}
}
![Trần Anh Quân [T1907A]](https://www.gravatar.com/avatar/7e3a8fe30cedd711f426fc59a9d48efc.jpg?s=80&d=mm&r=g)
Trần Anh Quân
2020-05-28 16:40:42
using System;
using System.Collections.Generic;
namespace Examination
{
class Program
{
static void Main(string[] args)
{
People people = new People();
Car car = new Car();
List<infor> a = new List<infor>();
people.input();
a.Add(people);
car.Input();
a.Add(car);
car.Input();
foreach (infor inn in a)
{
inn.Showinfor();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Examination
{
public interface infor
{
void Showinfor();
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Examination
{
class People : infor
{
public string Name { get; set; }
public int Age { get; set; }
public string Address { get; set; }
public People()
{
}
public void input()
{
Console.WriteLine("Nhap ten");
Name = Console.ReadLine();
Console.WriteLine("Nhap tuoi");
Age = Int32.Parse(Console.ReadLine());
Console.WriteLine("Nhap dia chi");
Address = Console.ReadLine();
}
public void Showinfor()
{
Console.WriteLine("{0},{1},{2},", Name, Age, Address);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Examination
{
class Car : infor
{
public string Name { get; set; }
public string ColorCar { get; set; }
public Car()
{
}
public void Input()
{
Console.WriteLine("Nhap ten Car");
Console.ReadLine();
Console.WriteLine("Nhap ColorCar");
Console.ReadLine();
}
public void Showinfor()
{
Console.WriteLine("{0},{1},", Name, ColorCar);
}
}
}
![Trương Công Vinh [T1907A]](https://www.gravatar.com/avatar/223a7e3a46f4a747f81b921fe023fcc4.jpg?s=80&d=mm&r=g)
Trương Công Vinh
2020-05-28 09:18:23
using System;
using System.Collections.Generic;
using System.Text;
namespace InterfacePeople
{
class Car:IInfor
{
public string name { get; set; }
public string color { get; set; }
public void input()
{
Console.WriteLine("Car:");
Console.Write("enter name of car: ");
name = Console.ReadLine();
Console.Write("enter color of car: ");
color = Console.ReadLine();
}
public void showInfor()
{
Console.WriteLine("Car:");
Console.WriteLine("Name: {0}", name);
Console.WriteLine("Color: {0}", color);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace InterfacePeople
{
class People : IInfor
{
public string name { get; set; }
public int age { get; set; }
public string address { get; set; }
public People()
{
}
public void input()
{
Console.WriteLine("People:");
Console.Write("enter youe name: ");
name = Console.ReadLine();
Console.Write("enter your age: ");
age = Convert.ToInt32(Console.ReadLine());
Console.Write("enter your address : ");
address = Console.ReadLine();
}
public void showInfor()
{
Console.WriteLine("People:");
Console.WriteLine("Name: {0}", name);
Console.WriteLine("Age: {0}", age);
Console.WriteLine("Address: {0}", address);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace InterfacePeople
{
interface IInfor
{
void showInfor();
}
}
using System;
using System.Collections.Generic;
namespace InterfacePeople
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
List<IInfor> inferList = new List<IInfor>();
People people = new People();
Car car = new Car();
people.input();
car.input();
inferList.Add(people);
inferList.Add(car);
showInfor(inferList);
}
public static void showInfor(List<IInfor> a)
{
foreach (IInfor infor in a)
{
infor.showInfor();
}
}
}
}
![Trần Mạnh Dũng [T1907A]](https://www.gravatar.com/avatar/ee4661d1f9133c241d6c8287c7ea8ceb.jpg?s=80&d=mm&r=g)
Trần Mạnh Dũng
2020-05-27 10:40:01
#Program.cs
using System;
using System.Collections.Generic;
namespace ExaminationTest
{
class Program
{
static void Main(string[] args)
{
People peole = new People();
Car car = new Car();
peole.input();
car.input();
List<IInfor> listInfor = new List<IInfor>();
listInfor.Add(peole);
listInfor.Add(car);
showInfor(listInfor);
}
static void showInfor(List<IInfor> listInfor)
{
foreach (var item in listInfor)
{
item.showInfor();
}
}
}
}
#People.cs
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
namespace ExaminationTest
{
class People : IInfor
{
public string name { get; set; }
public int age { get; set; }
public string address { get; set; }
public void input()
{
Console.WriteLine("=================");
Console.Write("Enter name: ");
name = Console.ReadLine();
Console.Write("Enter age: ");
age = int.Parse(Console.ReadLine());
Console.Write("Enter address: ");
address = Console.ReadLine();
}
public void showInfor()
{
Console.WriteLine("================");
Console.WriteLine("Name: {0} \nAge: {1} \nAddress: {2}", name, age, address);
}
}
}
#IInfor.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace ExaminationTest
{
interface IInfor
{
void showInfor();
}
}
#Car.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace ExaminationTest
{
class Car : IInfor
{
public string nameCar { get; set; }
public string colorCar { get; set; }
public void input()
{
Console.Write("Enter nameCar: ");
nameCar = Console.ReadLine();
Console.Write("Enter colorCar: ");
colorCar = Console.ReadLine();
}
void IInfor.showInfor()
{
Console.WriteLine("nameCar: {0} \ncolorCar: {1}", nameCar, colorCar);
}
}
}
![Phí Văn Long [T1907A]](https://www.gravatar.com/avatar/5db166b7b74443c5c221e4c0068d6da9.jpg?s=80&d=mm&r=g)
Phí Văn Long
2020-05-27 10:10:10
using System;
using System.Collections.Generic;
namespace Examination
{
class Program
{
static void Main(string[] args)
{
People people = new People();
Car car = new Car();
List<IInfor> a = new List<IInfor>();
people.Input();
a.Add(people);
car.Input();
a.Add(car);
showInfor(a);
}
public static void showInfor(List<IInfor>a)
{
foreach(IInfor infor in a)
{
infor.ShowInfor();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Examination
{
public class People : IInfor
{
public string FullName { get; set; }
public int Age { get; set; }
public string Address { get; set; }
public void Input()
{
Console.WriteLine("Nhap ten : ");
FullName = Console.ReadLine();
Console.WriteLine("Nhap tuoi : ");
Age = int.Parse(Console.ReadLine());
Console.WriteLine("Nhap dia chi : ");
Address = Console.ReadLine();
}
public void ShowInfor()
{
Console.WriteLine("Ten : {0}; Tuoi : {1}; Dia Chi : {2}",FullName,Age,Address);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Examination
{
public interface IInfor
{
void ShowInfor() { }
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Examination
{
class Car : IInfor
{
public string NameCar { get; set; }
public string Color { get; set; }
public void Input()
{
Console.WriteLine("Nhap ten o to : ");
NameCar = Console.ReadLine();
Console.WriteLine("Nhap mau : ");
Color = Console.ReadLine();
}
public void ShowInfor()
{
Console.WriteLine("Ten O to : {0}; Mau : {1}", NameCar, Color);
}
}
}