返回类型比方法更容易访问(错误) [英] return type is less accessible than method ( error)
本文介绍了返回类型比方法更容易访问(错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
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 ispublic
which means it is freely available.
The default access modifier for items within a class isprivate
- so since you don't specify it, your enumDirection
isprivate
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 thepublic
access forFindDirection
cannot be honoured as the method can't be used outside the class itself.
Add the wordpublic
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 yourenum
public also:
public enum Direction { N = 1, NE = 2, E = 3, SE = 4, S = 5, SW = 6, W = 7, NW = 8, Invalid = 0 };
这篇关于返回类型比方法更容易访问(错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文