黑马程序员技术交流社区
标题:
如何传递引用类型变量。
[打印本页]
作者:
loveywj
时间:
2014-1-20 09:22
标题:
如何传递引用类型变量。
本帖最后由 loveywj 于 2014-1-21 09:22 编辑
如何传递引用类型变量呀,最好用代码实现一下。
作者:
pozhenzi9010
时间:
2014-1-20 09:57
class Program
{
static void Main(string[] args)
{
Person p = new Person();
Test(p);
Console.ReadKey();
}
static void Test(Person p)
{
}
}
class Person
{
public Person()
{
Console.WriteLine("我是一个人");
}
}
Test方法传递了Person(引用类型)
作者:
dapeng111
时间:
2014-1-20 10:13
public class QuoteVariable {
public static void main(String[] args)
{
int[] arr = new int[3];
//这里是一个数组类型的引用,建立一个数组对象。new int[3]在堆中分配,引用数组名arr在栈中分配空间。arr指向了new int[3];
for(int i=0; i<arr.length; i++) //给数组中的第一个变量同值,并打印输出。
{
arr[i] = i+1;
System.out.println("a[" + i + "]=" + arr[i]);
}
Hello hello = new Hello(); //这是一个类类型的引用,创建一个类的对象。类名hello在栈中分配,指对在堆中的对象new Hello();
hello.Hello(); //调用新建立的实例化对象中的构造方法Hello()实现想要的功能。
}
}
class Hello //建立一个外部的类,用于在QuoteVariable类中建立对象
{
public void Hello() //构造方法
{
System.out.println("Hello My friend!");
}
}
复制代码
希望能帮到你!
作者:
王忠杰
时间:
2014-1-20 12:20
class Program
{
static void Main(string[] args)
{
Person p1 = new Person();[b][/b]
p1.Name = "张三";
p1.Age = 18;
p1.Sex='男';
p1.SayHello();
Console.ReadKey();
}
}
class Person
{
string name;
public string Name
{
get { return name; }
set { name = value; }
}
int age;
public int Age
{
get { return age; }
set { age = value; }
}
char sex;
public char Sex
{
get { return sex; }
set { sex = value; }
}
public void SayHello()
{
Console.WriteLine("大家好,我叫{0},我今年{1}岁了,我是{2}生", name, age, sex);
}
}
复制代码
作者:
loveywj
时间:
2014-1-21 09:27
dapeng111 发表于 2014-1-20 10:13
希望能帮到你!
谢谢啦。受益匪浅
作者:
loveywj
时间:
2014-1-21 09:31
王忠杰 发表于 2014-1-20 12:20
谢谢哈。原来是这样
作者:
李稳023
时间:
2014-1-23 15:42
class Person
{
public string name;
private char sex;
public char Sex
{
get { return sex; }
set {
if (value == '男' || value == '女')
{
sex = value;
}
else
{
sex = '男';
}
}
}
private int age;
public int Age
{
get { return age; }
set {
if (value >= 0)
{
age = value;
}
else
{
age = 0;
}
}
}
public void SayHello()
{
Console.WriteLine("我叫{0},我是{1}生,我今年{2}岁",name,sex,age);
}
class Program
{
static void Main(string[] args)
{
Person zsPerson = new Person();
Person lsPerson = new Person();
zsPerson.name = "张三";
zsPerson.Sex = '男';
zsPerson.Age=18;
lsPerson.name="李四";
lsPerson.Age=21;
lsPerson.Sex='女';
zsPerson.SayHello();
lsPerson.SayHello();
Console.ReadKey();
复制代码
这是基础视频里面有的啊
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2