我有一个 C# 类库,它将在 2 个不同的项目中使用。其中一个应该访问所有公共类和方法,另一个应该只访问某些类和方法。
什么是最好的解决方案?
在我继续之前,我想指出我已经问了一些关于 TypeScript、它的编译器以及它在其生命周期中已经实现和没有实现的问题以及 1.0 版的路线图
这个问题与TypeScript中public和private关键字的使用有关,以及它们与编译后的 JavaScript 有何关系。
考虑以下 TypeScript 类:
class Example {
private messageA: string;
public messageB: string;
constructor(message?: string) {
this.messageA = "private: " + message;
this.messageB = "public: " + message;
}
public showMessageA(): void {
alert(this.messageA);
}
private showMessageB(): void {
alert(this.messageB);
}
}
var example = new Example("Hello World");
Run Code Online (Sandbox Code Playgroud)
现在,当我输入example. 智能感知 ( TypeScript ) 告诉我我可以访问messageB, 和showMessageA,因为它们都是public. 然而,这种行为(虽然可能)在编译的 JavaScript 中并不明显。
这是我班级的 JavaScript …
javascript compiler-construction class access-modifiers typescript
我有这样的事情:
class Node
{
protected Node Parent
{
get; private set;
}
}
class NodeDerived : Node
{
void SomeMethod()
{
Node parentIterator = this.Parent;
while (parentIterator != null)
{
// ... some logic
parentIterator = parentIterator.Parent; // Here's the error
}
}
}
Run Code Online (Sandbox Code Playgroud)
但由于一些奇怪的原因,我无法访问parentIterator.Parent属性:
error CS1540: Cannot access protected member `Node.Parent' via a qualifier of type `Node'. The qualifier must be of type `NodeChild' or derived from it
Run Code Online (Sandbox Code Playgroud)
为什么会这样?顺便说一句,我还发现,虽然我可以访问this.Parent,但我无法访问((Node) this).Parent.
有人可以解释一下,为什么 System.Type 中的 GetProperty 方法对于声明为“内部”但适用于“公共”的属性返回 null。
internal class Test{
public string ALocal { get; set; }
internal string SLocal { get; set; }}
var test = new Test();
var testType = test.GetType();
var aProp = testType.GetProperty("ALocal"); => returns string Type
var sProp = testType.GetProperty("SLocal"); => returns null
Run Code Online (Sandbox Code Playgroud)
我了解内部或公共修饰符之间的差异。
所以我做了这个java文件A.java,
package alphabet;
public class A{
private String private_A;
String _A;
protected String protected_A;
public String public_A;
public A(){
private_A="Private A";
_A="Package Private A";
protected_A="Protected A";
public_A="Public A";
}
public static void main(String[] args) {
}
}
Run Code Online (Sandbox Code Playgroud)
和同一个包中的另一个类,
package alphabet;
import alphabet.A;
public class B{
void methodB1(){
}
public static void main(String[] args) {
A AinB = new A();
}
}
Run Code Online (Sandbox Code Playgroud)
当我编译时,B我无法实例化A. 这是为什么?A是一个公共类,并且B属于同一个包?不B应该做一个实例A吗?
这很noobish,但谢谢。
编辑:得到这些错误,
*@*:~/rand$ javac …Run Code Online (Sandbox Code Playgroud) 当我们写下面的代码
Stream.of(1,2,3,4,5).filter(i -> (i%2 == 0)).map( i -> i*i );
Run Code Online (Sandbox Code Playgroud)
表达式i -> (i%2 == 0)或i -> i*i将变成私有方法.
在我的用例中,编写了一个junit测试以确保没有方法是私有的(是啊,那是强制的),并且这些lambda表达式失败了.
有人可以提出一些建议,其中我不必更改junit来为lambda表达式添加一些排除,但是让这些表达式在内部创建受保护的方法吗?
我刚刚遇到了一段代码。在一种情况下,我无法使用其实例访问类的私有成员(这很好),但在其他情况下,我能够使用其不同的实例访问私有成员(属于同班)。谁能解释一下为什么会发生这种情况?
class Complex {
private double re, im;
public String toString() {
return "(" + re + " + " + im + "i)";
}
Complex(){}
/*Below c is different instance, still it can access re,im( has a private access)
without any error.why? */
Complex(Complex c) {
re = c.re;
im = c.im;
}
}
public class Main {
public static void main(String[] args) {
Complex c1 = new Complex();
Complex c2 = new Complex(c1);
System.out.println(c1.re); /* But getting an …Run Code Online (Sandbox Code Playgroud) 如何将 javascript 对象属性设为私有,并且Object.keys()不Object.getOwnPropertyNames()返回该属性?
我想将构造函数及其字段隐藏在一个类中,并且只使用伴随对象创建实例,但我无法实现。我有 Scala 2.13.3,它基于 java 8。这是一个代码示例:
斯卡拉
package X
object A {
def apply(x: Int): A = A(Seq(x))
}
case class A private(private val s: Seq[Int])
Run Code Online (Sandbox Code Playgroud)
甜菜
package Y
import X.A
class B {
val b = A(Seq.empty)
}
Run Code Online (Sandbox Code Playgroud)
虽然我只想让apply(x:Int)这段代码可见,但编译后的私有构造函数也是可见的。如何更改此代码以按预期工作?
scala access-modifiers private-constructor case-class companion-object
我得到了这个类,它对于我创建的每个实例显然都是可变的,但我想知道是否有某种包装器(或其他东西)来使这个类的一个特定对象不可变。例如Collections.unmodifiableList(beanList)。
class Animal {
private String name;
private String commentary;
public Animal(String nombre, String comentario) {
this.name = nombre;
this.commentary = comentario;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Animal animal = (Animal) o;
return Objects.equals(name, animal.name);
}
@Override
public int hashCode() {
return Objects.hash(name);
}
public String getName() {
return name;
}
public String getCommentary() {
return commentary;
}
public void …Run Code Online (Sandbox Code Playgroud) access-modifiers ×10
java ×4
.net ×2
c# ×2
javascript ×2
object ×2
oop ×2
case-class ×1
class ×1
immutability ×1
installation ×1
java-8 ×1
lambda ×1
mono ×1
protected ×1
scala ×1
typescript ×1