首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将指定的类(或此类中的变量)导入到另一个类?

如何将指定的类(或此类中的变量)导入到另一个类?
EN

Stack Overflow用户
提问于 2018-12-12 20:01:34
回答 2查看 60关注 0票数 0

我有三个类:第一类和第二类变量,如下所示:

头等舱

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project_1
{
    class First_class
    {
        int variable_1 = 1;
        int variable_2 = 1;
        int variable_3 = 3;
        string variable_4 = "test1"

    }
}

二等

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project_1
{
    class Second_class
    {
        int variable_5 = 5;
        int variable_6 = 6;
        int variable_7 = 7;
        string varaible_8 = "test_2"

    }
}

第三级为空:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project_1
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}
  1. 我应该怎么做才能使用头等舱的所有变量?
  2. 我应该怎么做才有可能只使用一些变量来自头等舱和一些变量从二等-在三等?
EN

回答 2

Stack Overflow用户

发布于 2018-12-12 20:21:54

如果要在First_class之外使用这些变量,则必须将它们声明为公共变量。

代码语言:javascript
复制
class First_class
{
    public static int variable_1 = 1; // This can be accessed from outside
    public int variable_2 = 1; // This can be accessed from outside too
    int variable_3 = 3; // This can not be accessed from outside
    string variable_4 = "test1"; // This also can not be accessed from outside
}

class Program
{
    static void Main(string[] args)
    {
        var o = new First_class();
        Console.Writeline(First_class.variable_1);
        Console.Writeline(o.variable_2);
        Console.Writeline(o.variable_3); // Error: variable_3 is private
    }
}
票数 2
EN

Stack Overflow用户

发布于 2018-12-12 20:41:40

1.这里有两种可能性:

( a)要使用三等舱头等舱的所有变量,就必须将它们全部公之于众。

( b)您可以使它们都是静态的、公共的或受保护的,但是在这种情况下,Program类需要从第一个类继承,这样看起来就像:

代码语言:javascript
复制
public class First_class
{
    protected static int variable_1 = 1;
    protected static int variable_2 = 1;
    protected static int variable_3 = 3;
    protected static string variable_4 = "test1";
}

class Program: First_class
{
    static void Main(string[] args)
    {
        Console.WriteLine(variable_1);
        Console.WriteLine(variable_2);
        Console.WriteLine(variable_3);
        Console.WriteLine(variable_4);
    }
 }

2.例子:

代码语言:javascript
复制
class Second_class
{
    int variable_5 = 5;
    int variable_6 = 6;
    int variable_7 = 7;
    public string varaible_8 = "test_2"; //only this field can be accessed
}

public class First_class
{
    public int variable_1 = 1;
    public int variable_2 = 1;
    protected int variable_3 = 3; // can be accessed via inheritance
    private string variable_4 = "test1"; // cannot be accessed due to privacy level
}

class Program
{
    static void Main(string[] args)
    {
        var obj1 = new First_class();
        Console.WriteLine(obj1.variable_1);
        Console.WriteLine(obj1.variable_2);

        var obj2 = new Second_class();
        Console.WriteLine(obj2.varaible_8);

    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53750528

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档