Thứ Hai, 3 tháng 3, 2014

Class in c# (2)

Bài 29. Khởi tạo mặc định và khởi tạo truyền tham biến

1. Tạo một lớp Date gồm:
- Trường số liệu: ba trường số nguyên: dd, mm, yy
- Phương thức: gồm 2 phương thức khởi tạo khác nhau
+ Khởi tạo mặc định, gán sẵn số liệu là 05/12/69.
+ Khởi tạo truyền biến thông qua các biến dd, mm, yy
2. Tại chương trình chính (dạng windows Forms)
- Thiết kế nút lệnh để thực hiện khởi tạo mặc định, hiện kết quả ra textbox.
- Thiết kế nút lệnh để thực hiện khởi tạo các giá trị ngày tháng năm thông qua 3 textbox do người dùng nhập giá trị thực. Kết quả hiện ra dữ liệu ở textboxkq.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _29.Class_KhoiTao1
{
classDate
    {
publicint dd, mm, yy;
public Date(): this(5, 12, 1969)
        {
        }
public Date(int dd, int mm, int yy)
        {
            khoitao(dd, mm, yy);
        }
publicvoid khoitao(int dd, int mm, int yy)
        {
this.dd = dd;
this.mm = mm;
this.yy = yy;
        }
    }
}

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 _29.Class_KhoiTao1
{
publicpartialclassForm1 : Form
    {
public Form1()
        {
            InitializeComponent();
        }

privatevoid btnDefault_Click(object sender, EventArgs e)
        {
Date a = newDate();
this.txta.Text = a.dd + "/" + a.mm + "/" + a.yy;
        }

privatevoid btnFunction_Click(object sender, EventArgs e)
        {
Date b = newDate(15, 12, 2014);
this.txtb.Text = b.dd + "/" + b.mm + "/" + b.yy;
        }

privatevoid button1_Click(object sender, EventArgs e)
        {
            Close();
        }

privatevoid Form1_Load(object sender, EventArgs e)
        {

        }
    }
}


Bài 30. Minh họa sự kế thừa, minh họa tính chất private và public các trường dữ liệu trong quá trình kế thừa.

1. Tạo lớp tàu có cấu trúc
classtau
    {
//Các trường dữ liệu
publicstring hangsx;
publicint nhienlieu;

//Khởi tạo mặc định
public tau()
        {
            hangsx = "VinaLine";
            nhienlieu = 10;
}
    }

2. Tạo lớp tàu chiến, kế thừa lớp tàu:
classtauchien:tau
    {   //Khai báo trường dữ liệu
//2 trường private và 1 trường public, các trường khác kế thừa từ lớp tau
//trường private là trường riêng, nên muốn truy cập đến
//2 trường này cần phải định nghĩa hàm thuộc tính có chứa get và set
//2 trường riêng này các lớp dẫn xuất không kế thừa được
privatestring sung;
privatestring dan;
publicstring chieudai;

//thuộc tính Sung để tác động đến trường sung
publicstring Sung
        {
set { sung = value; }
get { return sung; }
        }

//thuộc tính Dan để tác động đến trường dan
publicstring Dan
        {
set { dan = value; }
get { return dan; }
        }

//Khởi tạo giá trị mặc định của tàu chiến
public tauchien()
        {
            sung = "12ly7";
            dan = "Lien thanh";
            chieudai = "100m";
        }
    }

3. Tạo lớp tàu chở hàng, kế thừa lớp tàu chiến
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ct24_kethua
{
classtauchohang:tauchien
    {
//Gồm 1 trường dữ liệu
//Các dữ liệu khác kế thừa từ lớp tauchien
publicint noichuahang;
public tauchohang()
{
        noichuahang=1500;
    }

    }
}

4. Tại chương trình chính thực hiện
classProgram
    {
staticvoid Main(string[] args)
{
tau a = newtau();
tauchien b = newtauchien();
tauchohang c = newtauchohang();
Console.WriteLine("Tau thong thuong:");
Console.WriteLine("Hang san xuat: {0}, Nhien lieu mac dinh {1}", a.hangsx, a.nhienlieu );
Console.WriteLine("------------------------");

//Lớp tàu chiến
//Do trường sung va dan là 2 trường private nên muốn lấy dữ liệu phải thông qua 2 phương thức Sung và Dan
//hangsx và nhienlieu kế thừa từ lớp tau sang
Console.WriteLine("TAU CHIEN:");
Console.WriteLine("Hang san xuat: {0}, Nhien lieu mac dinh {1}", b.hangsx,b.nhienlieu);
Console.WriteLine("Sung: {0}, Dan: {1}", b.Sung, b.Dan);
Console.WriteLine("Chieu dai: {0} ", b.chieudai);
Console.WriteLine("------------------------");


//Lớp tàu chở hàng
//c.chieudai kế thừa từ lớp tàu chiến
//c.nhienlieu và c.hangsx kế thừa bắc cầu thông qua lớp tàu chiến đến lớp tàu.
Console.WriteLine("TAU CHO HANG:");
Console.WriteLine("Hang san xuat: {0}, Nhien lieu mac dinh {1}", a.hangsx,c.nhienlieu);
Console.WriteLine("Noi chua hang: {0}", c.noichuahang);
Console.WriteLine("Chieu dai: {0} ", c.chieudai);


Console.ReadKey();

        }
    }


Bài 31.

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

namespace ct24_kethua
{
classtau
    {
publicint nhienlieu;
publicstring hangsx;
public tau()
        {
            hangsx = "VinaLine";
            nhienlieu = 10;
}
    }
}

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

namespace ct24_kethua
{
classtauchien:tau
    {
publicstring sung;
publicstring dan;
public tauchien()
        {
            sung = "12ly7";
            dan = "Lien thanh";
        }
    }
}

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

namespace ct24_kethua
{
classtauchohang:tauchien
    {
publicint noichuahang;
public tauchohang()
    {
        noichuahang=1500;
    }

    }
}

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

namespace ct24_kethua
{
classProgram
    {
staticvoid Main(string[] args)
{
tau a = newtau();
tauchien b = newtauchien();
tauchohang c = newtauchohang();
Console.WriteLine("Tau thong thuong:");
Console.WriteLine("Hang san xuat: {0}, Nhien lieu mac dinh {1}", a.hangsx, a.nhienlieu );
Console.WriteLine("------------------------");
Console.WriteLine("TAU CHIEN:");
Console.WriteLine("Hang san xuat: {0}, Nhien lieu mac dinh {1}", b.hangsx,b.nhienlieu);
Console.WriteLine("Sung: {0}, Dan: {1}", b.sung, b.dan);
Console.WriteLine("------------------------");
Console.WriteLine("TAU CHO HANG:");
Console.WriteLine("Hang san xuat: {0}, Nhien lieu mac dinh {1}", a.hangsx,c.nhienlieu);
Console.WriteLine("Noi chua hang: {0}", c.noichuahang);
Console.ReadKey();

        }
    }

}

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

Đăng nhận xét