超类包含方法C#
本文关键字:方法 包含 超类 | 更新日期: 2023-09-27 18:19:41
我已经添加了讲师类的代码,请告诉我它是否相关添加Person.cs`在试图向数据库中添加讲师时,我在试图弄清楚如何访问Address.cs类的集合和获取方法时遇到了问题。讲师继承自Person.cs,其中Person包含Address对象,因此可以使用Address.cs的set和get方法。但是,我不能在我的讲师类中使用这些set和get方法,只能向讲师添加Address对象,或者在Person中包含/覆盖set/get。我的问题是,在讲师类的代码中设置地址(String street,String city,String state,int Zip)的正确/最佳方式是什么?这样我就可以在数据库中使用get方法插入查询
"INSERT * where Id=(" ...........get methods")"
/*********************************
* William Tolliver
* June 18, 2013
* C# lab #2 Gui
* instructor Ronald Enz
* *******************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrjLab2
{
class Instructor : Person
{
#region Fields
public System.Data.OleDb.OleDbDataAdapter OleDbDataAdapter;
public System.Data.OleDb.OleDbCommand OleDbSelectCommand;
public System.Data.OleDb.OleDbCommand OleDbInsertCommand;
public System.Data.OleDb.OleDbCommand OleDbUpdateCommand;
public System.Data.OleDb.OleDbCommand OleDbDeleteCommand;
public System.Data.OleDb.OleDbConnection OleDbConnection;
private String office;
public String cmd;
private Address addr;
private Schedule sch;
#endregion
#region Constructors
public Instructor() { }
public Instructor(int i) { SelectDB(i); }
public Instructor(int i, String f, String l, Address add, Schedule sc, String o, String e)
: base(f, l, e, i, add, sc)
{
office = o;
addr = add;
sch = sc;
insertDB();
}
#endregion
#region Set/Get methods
public String Office
{
get { return office; }
set { office = value; }
}
#endregion
#region Display method
public override void display()
{
Console.WriteLine(" First Name: " + FName + "'n Last Name: " + LName + "'n ID: " + Id + "'n" + addr.ToString() + "'n Office: " + Office + "'n Email:" + Email);
}
#endregion
#region Database setup
//============================= BEHAVIORS =========================
//++++++++++++++++ DATABASE Data Elements +++++++++++++++++
public void DBSetup()
{
// +++++++++++++++++++++++++++ DBSetup function +++++++++++++++++++++++++++++++
OleDbDataAdapter = new System.Data.OleDb.OleDbDataAdapter();
OleDbSelectCommand = new System.Data.OleDb.OleDbCommand();
OleDbInsertCommand = new System.Data.OleDb.OleDbCommand();
OleDbUpdateCommand = new System.Data.OleDb.OleDbCommand();
OleDbDeleteCommand = new System.Data.OleDb.OleDbCommand();
OleDbConnection = new System.Data.OleDb.OleDbConnection();
//OleDbDataAdapter1
OleDbDataAdapter.DeleteCommand = OleDbDeleteCommand;
OleDbDataAdapter.InsertCommand = OleDbInsertCommand;
OleDbDataAdapter.SelectCommand = OleDbSelectCommand;
OleDbDataAdapter.UpdateCommand = OleDbUpdateCommand;
OleDbConnection.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database L" +
"ocking Mode=1;Data Source=C:''Users''William''Documents''Visual Studio 2012''Lab3''RegistrationDB.mdb;J" +
"et OLEDB:Engine Type=5;Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:System datab" +
"ase=;Jet OLEDB:SFP=False;persist security info=False;Extended Properties=;Mode=S" +
"hare Deny None;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;Jet " +
"OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repai" +
"r=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1";
} //end DBSetup()
#endregion
#region Database selection
public void SelectDB(int i)
{ //++++++++++++++++++++++++++ SELECT +++++++++++++++++++++++++
DBSetup();
cmd = "Select * from Instructors where ID = " + i;
OleDbDataAdapter.SelectCommand.CommandText = cmd;
OleDbDataAdapter.SelectCommand.Connection = OleDbConnection;
Console.WriteLine(cmd);
try
{
OleDbConnection.Open();
System.Data.OleDb.OleDbDataReader dr;
dr = OleDbDataAdapter.SelectCommand.ExecuteReader();
dr.Read();
Id = i;
FName = dr.GetValue(1).ToString();
LName = dr.GetValue(2).ToString();
addr = new Address(dr.GetValue(3).ToString(), dr.GetValue(4).ToString(), dr.GetValue(5).ToString(), Int32.Parse(dr.GetValue(6).ToString()));
Office = dr.GetValue(7).ToString();
Email = dr.GetValue(8).ToString();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
OleDbConnection.Close();
}
} //end SelectDB()
#endregion
#region Database insertion
public void insertDB()
{
DBSetup();
cmd = "INSERT into Instructors values(" + Id + "," + "'" + FName + "'," + "'" + LName + "'," + "'" + addr.Street + "'," + "'" + addr.City + "'," + "'" + addr.State + "'," + "'" + addr.Zip + "'," + "'" + Office + "'," + "'" + Email + "'" + ")";// cmd = "INSERT into Instructors values( Id, FName, LName, addr.Street, addr.City, addr.State,addr.Zip,Office,Email )";
OleDbDataAdapter.InsertCommand.CommandText = cmd;
OleDbDataAdapter.InsertCommand.Connection = OleDbConnection;
Console.WriteLine(cmd);
try
{
OleDbConnection.Open();
int n = OleDbDataAdapter.InsertCommand.ExecuteNonQuery();
if (n == 1)
Console.WriteLine("Data Inserted");
else
Console.WriteLine("ERROR: Inserting Data");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
OleDbConnection.Close();
}
} //end InsertDB()
#endregion
}
}
`
如果看不到代码,很难诊断。但是,您是否已将属性方法设置为public
。
例如:
public class Person
{
public Address Address { get; set; }
...
}
public class Instructor : Person
{
public void DoSomething()
{
Address.City = "Wellington";
}
}
如果您想从子类访问这些属性,但要访问其他属性。这个怎么样
public class Person
{
protected Address Address {get;set;}
}
public class Instructor : Person
{
public void Instructor (int i, String f, String l, Address add, Schedule sc, String o, String e)
{
Address = add;
}
}
这里的重点是Person
类中的protected
修饰符。您可以从这里了解更多