关于类的问题。

Ganz 2003-10-19 12:34:49
有两个类
public class User
{
public int userid;
public string username;
public bool islogin()
{}
}
public class Shop
{
public int shopid;
public string shopname;
public bool shopisexist()
{}
}
如何做到在User实例化比如User user =new User();后可以用
int x=user.shop.shopid;
int y=user.userid;
bool y=user.shop.shopisexist();

第一次接触类,请指教。
...全文
27 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
coudoufu 2003-10-19
  • 打赏
  • 举报
回复
int x=user.shop.shopid;
可以通过把Shop定义为结构来实现,
int y=user.userid;本来就可以直接引用的,
bool y=user.shop.shopisexist();没有这样的搞法吧,
直接shop.shopisexist();就可以了.
aloxy 2003-10-19
  • 打赏
  • 举报
回复
示例

using System;
class MyClass
{
public MyClass() {
Console.WriteLine("Instance constructor");
}
public MyClass(int value) {
MyField = value;
Console.WriteLine("Instance constructor");
}
~MyClass() {
Console.WriteLine("Destructor");
}
public const int MyConst = 12;
public int MyField = 34;
public void MyMethod(){
Console.WriteLine("MyClass.MyMethod");
}
public int MyProperty {
get {
return MyField;
}
set {
MyField = value;
}
}
public int this[int index] {
get {
return 0;
}
set {
Console.WriteLine("this[{0}] = {1}", index, value);
}
}
public event EventHandler MyEvent;
public static MyClass operator+(MyClass a, MyClass b) {
return new MyClass(a.MyField + b.MyField);
}
internal class MyNestedClass
{}
}
显示了一个类,它包含了具有各种可访问性的成员。示例

class Test
{
static void Main() {
// Instance constructor usage
MyClass a = new MyClass();
MyClass b = new MyClass(123);
// Constant usage
Console.WriteLine("MyConst = {0}", MyClass.MyConst);
// Field usage
a.MyField++;
Console.WriteLine("a.MyField = {0}", a.MyField);
// Method usage
a.MyMethod();
// Property usage
a.MyProperty++;
Console.WriteLine("a.MyProperty = {0}", a.MyProperty);
// Indexer usage
a[3] = a[1] = a[2];
Console.WriteLine("a[3] = {0}", a[3]);
// Event usage
a.MyEvent += new EventHandler(MyHandler);
// Overloaded operator usage
MyClass c = a + b;
}
static void MyHandler(object sender, EventArgs e) {
Console.WriteLine("Test.MyHandler");
}
internal class MyNestedClass
{}
}
Ganz 2003-10-19
  • 打赏
  • 举报
回复


感谢解答。


rgbcn 2003-10-19
  • 打赏
  • 举报
回复
bool y=user.shop.shopisexist();
没有这种用法的。
Stevetan81 2003-10-19
  • 打赏
  • 举报
回复
在user类中做一个公开的shop实例
zhehui 2003-10-19
  • 打赏
  • 举报
回复
public class User
{
public int userid;
public string username;
public bool islogin()
{}
public Shop shop=new Shop();
}
public struct Shop
{
public int shopid;
public string shopname;
public bool shopisexist()
{}
}

将public class Shop改成public struct Shop这样也可以。

Jack0Cao 2003-10-19
  • 打赏
  • 举报
回复
SHOP做成结构替
USER变成类
donger2000 2003-10-19
  • 打赏
  • 举报
回复
HunterForPig(留着口水的猪) 的方法就可以了
HunterForPig 2003-10-19
  • 打赏
  • 举报
回复
public class User
{
public int userid;
public string username;
public bool islogin()
{}
public Shop shop=new Shop();
}
public class Shop
{
public int shopid;
public string shopname;
public bool shopisexist()
{}
}

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧