Thứ Hai, 3 tháng 3, 2014

Class in c# (30-31-32)

Bài 32.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ct24_kethua
{
classtau
    {
publicint nhienlieu;
public tau()
        {
            nhienlieu = 10;
        }
public tau(int x)
        {
            nhienlieu = x;
        }
//Hàm đốt nhiên liệu sẽ có kế thừa từ lớp tàu chiến
publicvoid dotnhienlieu(int x)
        {
            nhienlieu=nhienlieu-x;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//Ke thua, su dung ham base de lua chon ham tao
namespace ct24_kethua
{
classtauchien:tau//tàu chiến kế thừa từ tàu
{
publicint sung;
publicint dan;
public tauchien(int x, int y, int z):base(z)
{
            sung = x;
            dan = y;
        }
//Ham ban se ke thua ham dotnhienlieu tu lop Co so
publicvoid ban()
        {
base.dotnhienlieu(5);
            dan = dan - 1;
}
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//Ke thua, su dung ham base de lua chon ham tao
namespace ct24_kethua
{
classProgram
    {
staticvoid Main(string[] args)
{
tau a = newtau();
tauchien b = newtauchien(1,500,200); //dùng hàm khởi tạo
Console.WriteLine("----Tau ban dau-------");
Console.WriteLine(a.nhienlieu);
Console.WriteLine("----Tau Chien-------");
Console.WriteLine("sung={0}",b.sung);
Console.WriteLine("dan={0}",b.dan);
Console.WriteLine("nhienlieu={0}",b.nhienlieu);
            b.ban();
Console.WriteLine("----Tau chien sau khi ban-------");
Console.WriteLine("sung={0}", b.sung);
Console.WriteLine("dan={0}", b.dan);
Console.WriteLine("nhienlieu={0}", b.nhienlieu);

Console.ReadKey();

        }
    }
}

Bài 33.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace _32.Class_TauThuy_Dahinh
{
publicpartialclassForm1 : Form
    {
public Form1()
        {
            InitializeComponent();
        }
classtau//Lớp dẫn xuất
        {
publicvirtualvoid Laythongtin()
            {
MessageBox.Show("Đây là tàu lớp cơ sở ");
            }
        }
classtauchien : tau//kế thừa
        {
publicoverridevoid Laythongtin()
            {
MessageBox.Show("Đây là tàu chiến");
            }
        }

classtauchohang : tau//kế thừa
        {
publicoverridevoid Laythongtin()
            {
MessageBox.Show("Đây là tàu chờ hàng");
            }
        }
privatevoid Form1_Load(object sender, EventArgs e)
        {
this.txtMinhHoa.Text = "Ba lớp (tàu,tàu chiến, tàu chở hàng) đều chứa hàm Laythongtin(). ";
this.txtMinhHoa.Text = this.txtMinhHoa.Text + "Lớp tàu chiến kế thừa Lớp tàu, Lớp tàu chở hàng kế thừa Lớp tàu. " ;
this.txtMinhHoa.Text = this.txtMinhHoa.Text + "Định nghĩa một mảng tàu, và gán cho các phần tử của mảng là các loại tàu khác nhau. ";
this.txtMinhHoa.Text = this.txtMinhHoa.Text + "Gọi thực hiện mangtau[i].LayThongtin() cho kết quả đúng theo loại tàu. ";
this.txtMinhHoa.Text = this.txtMinhHoa.Text + "Tính đa hình đã được thể hiện.";
string filename;
            filename = "Form1.cs";
            txtContent.Text = File.ReadAllText(filename);
        }
privatevoid btnThucHien_Click(object sender, EventArgs e)
{
tau[] mangtau = newtau[3];
            mangtau[0] = newtau();
            mangtau[1] = newtauchien();
            mangtau[2] = newtauchohang();
for (int i = 0; i < 3; i++)
            {
                mangtau[i].Laythongtin();
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace _32.Class_TauThuy_Dahinh
{
staticclassProgram
    {
///
/// The main entry point for the application.
///

        [STAThread]
staticvoid Main()
        {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(newForm1());
        }
    }
}


Bài 34.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BaiThucHanhLop
{
classStudent
    {
privatestring _hoTen;
privateint _tuoi;
privatedouble _diemToan;
privatedouble _diemVan;
privatedouble _dtb;
//Hàm khởi tạo không có tham số
public Student()
        {
            HoTen = "";
            DiemVan = 0;
            DiemToan = 0;
            Dtb = 0;
        }
//Các phương thức Properties để get/set giá trị cho các thuộc tính
publicstring HoTen
        {
get { return _hoTen; }
set { _hoTen = value; }
        }
publicint Tuoi
        {
get { return _tuoi; }
set { _tuoi = value; }
        }
publicdouble DiemToan
        {
get { return _diemToan; }
set { _diemToan = value; }
        }
publicdouble DiemVan
        {
get { return _diemVan; }
set { _diemVan = value; }
        }
publicdouble Dtb
        {
get { returnMath.Round(((DiemToan + DiemVan) / 2), 2); }
set { _dtb = value; }
        }
//Các phương thức nhập/xuất dữ liệu                   

publicvoid nhap()
        {
Console.Write(" \t -Nhap ho ten:");
            HoTen = Console.ReadLine();
Console.Write(" \t -Nhap diem toan:");
Double temp;
temp = double.Parse(Console.ReadLine());
if (temp > 10 || temp < 0)
{
Console.WriteLine(" \t !!! Diem phai nam trong khoang 0 -> 10");
Console.Write(" \t -Nhap lai diem toan:");
                temp = double.Parse(Console.ReadLine());
            }
            DiemToan = temp;

Console.Write(" \t -Nhap diem van:");
            temp = double.Parse(Console.ReadLine());
if (temp > 10 || temp < 0)
            {
Console.WriteLine(" \t -Diem phai nam trong khoang 0 -> 10");
Console.Write(" \t -Nhap lai diem Van:");
                temp = double.Parse(Console.ReadLine());
            }
            DiemVan = temp;

        }
publicvoid xuat()
{
Console.WriteLine("{0,-15}{1,-15}{2,-15}{3,-15}", HoTen, DiemToan, DiemVan, Dtb);
        }
    }

classProgram
    {
publicstaticvoid Main()
        {
int n;
Console.Write(" Nhap so luong hoc sinh: ");
            n = int.Parse(Console.ReadLine());

Student[] _arrStudent = newStudent[n];
for (int i = 0; i < n; i++)
            {
Console.WriteLine(" Nhap thong tin sinh vien thu: " + (i + 1).ToString());
                _arrStudent[i] = newStudent();
                _arrStudent[i].nhap();
            }

Console.WriteLine(" Danh sach hoc sinh: ");
Console.WriteLine("{0,-15}{1,-15}{2,-15}{3,-15}", "Ho Ten", "Diem Toan", "Diem Van", "DTB");

for (int i = 0; i < n; i++)
            {
                _arrStudent[i].xuat();
            }
Console.ReadLine();
        }
    }

}

Không có nhận xét nào:

Đăng nhận xét