标题:ref的值传递问题。。。。
只看楼主
黄辉
Rank: 4
等 级:业余侠客
帖 子:106
专家分:255
注 册:2013-4-28
结帖率:100%
已结贴  问题点数:35 回复次数:2 
ref的值传递问题。。。。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public class TuXing    //父类
    {
        double ZhouChang;               //定义周长为私有的
        double MianJi;                  //定义面积为私有的
        public double MyZhouChang       //定义名为MyZhouChang的周长属性
        {
            get                         //get属性访问器
            {
                return  ZhouChang;
            }
            set                         //set属性访问器
            {
                ZhouChang = value;
            }
        }
        public double MyMianJi      //定义名为MyMianJi 的面积属性
        {
            get                     //get属性访问器
            {
                return MianJi ;
            }
            set                     //set属性访问器
            {
                MianJi= value;
            }
        }
   
    }
    public class TuXing1 : TuXing
    {
        public void  JuXing(ref double a,ref double b)         //定义几何图形为矩形
        {
            MyZhouChang =(a+b); //周长
           
            MyMianJi = a * b;    //面积
         
        }
        public void SanJiaoXing(ref double A,ref  double B,ref  double C)      //定义几何图形为三角形
        {
            double s = (A + B + C) / 2;
            MyZhouChang = A + B + C;    //周长
  
            MyMianJi = (Math.Sqrt(s * (s - A) * (s - B) * (s - C)));    //面积
         
        }
        public void   Yuan(ref double R)    //定义几何图形为圆
        {
            MyZhouChang = 2 * (Math.PI) * R; //周长

            MyMianJi =(Math .PI )*(Math.Sqrt(R));     //面积
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
   
            double a;
            double b;
            double c;
            double MianJi;
            double ZhouChang;
           
            TuXing1 TX=new TuXing1 ();
            MianJi = TX.MyMianJi ;
            ZhouChang = TX.MyZhouChang;
            Random rand = new Random();
            a = rand.Next(1, 20);
            b = rand.Next(1, 20);
            c = rand.Next(1, 20);
            Console.WriteLine("a=" + a + " " + "b=" + b + " " + "c=" + c + " ");

             TX.JuXing( ref a, ref  b);
            Console.Write("矩形的周长及面积分别为:");
            Console.WriteLine(ZhouChang + "  " + MianJi);
            TX.Yuan(ref  a);
            Console.Write("圆的周长及面积分别为:");
            Console.WriteLine(ZhouChang + "  " + MianJi);

            if ((a - b < c && a + b > c) && (a - c < b && a + c > b) && (b - c < a && b + c > a))
            {
               TX.SanJiaoXing(ref  a,ref b,ref  c);
            }
            Console.Write("三角形的周长及面积分别为:");
            Console.WriteLine(ZhouChang + "  " + MianJi);

        }
    }
}
请问哪里错了????输出的结果都为0。,路过的朋友帮帮忙啊。。。。
搜索更多相关主题的帖子: namespace public double return 
2013-05-23 18:34
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
得分:35 
你最后输出的是zhouchang,mianji
ref a b c有什么用呢,你最后又不是输出a b c,且a b c的值也没有改变过
简单的改改嘛,就是这样
static void Main(string[] args)
        {
            double a;
            double b;
            double c;
            double MianJi;
            double ZhouChang;

            TuXing1 TX = new TuXing1();
            
            Random rand = new Random();
            a = rand.Next(1, 20);
            b = rand.Next(1, 20);
            c = rand.Next(1, 20);
            Console.WriteLine("a=" + a + " " + "b=" + b + " " + "c=" + c + " ");

            TX.JuXing(ref a, ref  b);
            MianJi = TX.MyMianJi;
            ZhouChang = TX.MyZhouChang;
            Console.Write("矩形的周长及面积分别为:");
            Console.WriteLine(ZhouChang + "  " + MianJi);
            TX.Yuan(ref  a);
            MianJi = TX.MyMianJi;
            ZhouChang = TX.MyZhouChang;
            Console.Write("圆的周长及面积分别为:");
            Console.WriteLine(ZhouChang + "  " + MianJi);

            if ((a - b < c && a + b > c) && (a - c < b && a + c > b) && (b - c < a && b + c > a))
            {
                TX.SanJiaoXing(ref  a, ref b, ref  c);
            }
            MianJi = TX.MyMianJi;
            ZhouChang = TX.MyZhouChang;
            Console.Write("三角形的周长及面积分别为:");
            Console.WriteLine(ZhouChang + "  " + MianJi);

        }

按照原本你的意思的话,应该把 周长和面积作为ref参数传进去,并且在方法里面给这两个参数赋值,至于a b c到没有必要用ref传进去
2013-05-23 19:10
黄辉
Rank: 4
等 级:业余侠客
帖 子:106
专家分:255
注 册:2013-4-28
得分:0 
谢谢。。。。。刚开始还想不通,后来就想通了,真的很感谢你,我终于明白怎么用ref传值了。。。。

你不勇敢,谁替你坚强。。。
2013-05-23 22:01



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-407855-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.367565 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved