返回类型比方法更容易访问(错误) [英] return type is less accessible than method ( error)

查看:81
本文介绍了返回类型比方法更容易访问(错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



i有一个方法,返回类型是枚举,但它给我的错误是返回类型比方法更难以访问如何解决它请。?



hi all,

i have a method having return type is enum , but it give me the error that return type is less accessible than method how to solve it please.?

enum Direction { N = 1, NE = 2, E = 3, SE = 4, S = 5, SW = 6, W = 7, NW = 8, Invalid = 0 };

public Direction FindDirection(Point p1, Point p2)
       {
           Direction d = Direction .Invalid;


           if (p1.X < p2.X && p1.Y < p2.Y)
               d = Direction.SE;
           if (p1.X < p2.X && p1.Y == p2.Y)
               d = Direction.E;
           if (p1.X < p2.X && p1.Y > p2.Y)
               d = Direction.NE;
           if (p1.X > p2.X && p1.Y == p2.Y)
               d = Direction.W;
           if (p1.X > p2.X && p1.Y < p2.Y)
               d = Direction.SW;
           if (p1.X > p2.X && p1.Y > p2.Y)
               d = Direction.NW;
           if (p1.X == p2.X && p1.Y > p2.Y)
               d = Direction.N;
           if (p1.X == p2.X && p1.Y < p2.Y)
               d = Direction.S;

           return d;

       }

推荐答案

该方法 public 这意味着它是免费提供的。

类中项目的默认访问修饰符是 private - 所以既然你没有指定它,您的枚举 Direction private 到您的类,这意味着它不能在您声明的类之外使用中的方法。



由于返回类型不能在类外指定方法,编译器会告诉你 public 访问 FindDirection 无法兑现,因为该方法不能在类本身之外使用。



在此添加单词 public

The method is public which means it is freely available.
The default access modifier for items within a class is private - so since you don't specify it, your enum Direction is private to your class, meaning it can't be used outside the class you declared the method in.

Since the return type ot the method cannot be specified outside the class the compiler is telling you that the public access for FindDirection cannot be honoured as the method can't be used outside the class itself.

Add the word public to this:
public enum Direction { N = 1, NE = 2, E = 3, SE = 4, S = 5, SW = 6, W = 7, NW = 8, Invalid = 0 };

一切都会好的。


你需要让你的 enum 公开:

You need to make your enum public also:
public enum Direction { N = 1, NE = 2, E = 3, SE = 4, S = 5, SW = 6, W = 7, NW = 8, Invalid = 0 };


这篇关于返回类型比方法更容易访问(错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆