using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace chapter3._1
{
class Program
{
static void Main(string[] args)
{
//Usefor t = new Usefor();
//t.show();
//Car t = new Car(2,3);
//t.Info();
Console.Read();
}
}
}
在这个页面怎么样调用Stu类,
—
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace chapter3._1
{
class Car
{
int wheels;
protected float weight;
public Car (int w,float g)
{
wheels=w;
weight =g;
}
~Car()
{
}
public void Info()
{
Console.WriteLine(“{0}轮子”,wheels);
Console.WriteLine(“{0}kg重量”,weight);
}
public void Sprak()
{
Console.WriteLine(“能够加速”);
}
class stu
{
int sn;
float weight;
string name;
string sex;
public stu(int s,float w,string n,string se)
{
sn =s;
weight=w;
name =n;
sex =se;
}
~stu()
{
}
public void StuInfo()
{
Console.WriteLine(“学号:{0}”,sn);
Console.WriteLine(“体重:{0}”,weight);
Console.WriteLine(“姓名:{0}”,name);
Console.WriteLine(“性别:{0}”,sex);
}
};
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace chapter3._1
{
class Program
{
static void Main(string[] args)
{
//Usefor t = new Usefor();
//t.show();
//Car t = new Car(2,3);
//t.Info();
Console.Read();
}
}
}
在这个页面怎么样调用Stu类,
—
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace chapter3._1
{
class Car
{
int wheels;
protected float weight;
public Car (int w,float g)
{
wheels=w;
weight =g;
}
~Car()
{
}
public void Info()
{
Console.WriteLine(“{0}轮子”,wheels);
Console.WriteLine(“{0}kg重量”,weight);
}
public void Sprak()
{
Console.WriteLine(“能够加速”);
}
class stu
{
int sn;
float weight;
string name;
string sex;
public stu(int s,float w,string n,string se)
{
sn =s;
weight=w;
name =n;
sex =se;
}
~stu()
{
}
public void StuInfo()
{
Console.WriteLine(“学号:{0}”,sn);
Console.WriteLine(“体重:{0}”,weight);
Console.WriteLine(“姓名:{0}”,name);
Console.WriteLine(“性别:{0}”,sex);
}
};
}
}
解决方案
3
namespace chapter3._1 { class Program { static void Main(string[] args) { stu s=new stu(1001,70.5,"张三","男"); s.StuInfo(); Console.Read(); } } }
2
stu在car中,嵌套类,简单点就是把stu定义在car外面,就能直接实例访问了。
3
public class stu { int sn; float weight; string name; string sex; public stu(int s, float w, string n, string se) { sn = s; weight = w; name = n; sex = se; } ~stu() { } public void StuInfo() { Console.WriteLine("学号:{0}", sn); Console.WriteLine("体重:{0}", weight); Console.WriteLine("姓名:{0}", name); Console.WriteLine("性别:{0}", sex); } } static void Main(string[] args) { stu s = new stu(1001, 70.5f, "张三", "男"); s.StuInfo(); Console.Read(); }
10
不改动位置就按下面调用
static void Main(string[] args) { Car.stu s = new Car.stu(1001, 70.5f, "张三", "男"); //表示是car中的stu s.StuInfo(); Console.Read(); }
必须修改
public class stu //添加public
2
类成员的访问修饰符默认是private
所以stu的定义类外部应该是不可见的
要么你把stu的定义提到外面去,要么注明public
个人不喜欢嵌套声明类,一个cs文件声明多个类。
所以stu的定义类外部应该是不可见的
要么你把stu的定义提到外面去,要么注明public
个人不喜欢嵌套声明类,一个cs文件声明多个类。