【问题标题】:Instantiate Variables In a Class在类中实例化变量
【发布时间】:2020-12-03 07:56:42
【问题描述】:

我目前正在开发一个基于 Ionic / Angular 的应用程序,并且想知道实例化变量的不同方法实际上意味着什么。已经完成并看到了几种不同的方式,但我不确定这三种情况中的任何一种实际做了什么:

export class someClass {
  myVariable: string
  ...

  someFunction() {
     // Accessed without issue without needing to instantiate.
     this.myVariable = 'foo';
  }
}

当这样做时,它只是给了我一个默认的字符串对象,然后我用“foo”覆盖它吗?

export class someClass {
  appService: AppService
  constructor(appService: AppService) {
     // No idea what this means as opposed to excluding it from the constructor.
     this.appService = appService;
  }
}

当以这种方式完成时,我实际上将 this.appService 设置为什么?这只是重复/冗余实例吗?

export class someClass {
  constructor(private storage: Storage) {}
  
  async getSomeVariable(): Promise<any> {
     // Mentioned as a parameter in the constructor 
    // but never set to anything or defined as a variable outside of the constructor?
     return await this.storage.get('someVariable');
  }
}

以这种方式完成时,它只是实例化“存储”的默认实例,这就是我稍后在函数中访问的内容吗?

【问题讨论】:

    标签: angular typescript ionic-framework


    【解决方案1】:

    在你的第一个例子中:

    export class someClass {
      myVariable: string
      ...
    
      someFunction() {
         // Accessed without issue without needing to instantiate.
         this.myVariable = 'foo';
      }
    }
    

    someClass 有一个名为myVariable 的属性,它正急切地等待着为您保留一些价值。目前,它是undefined。 在someFunction(),它实现了人生的目的,终于可以为你保住foo的价值。它的生命现在已经完成。 这是在大多数语言中都可以找到的基本的标准类级别属性。例如。 C#:

    public class SomeClass {
      private string _myVariable;
    
      public void SomeFunction() {
        _myVariable = "foo";
      }
    }
    

    你的第二个例子:

    export class someClass {
      appService: AppService
      constructor(appService: AppService) {
         // No idea what this means as opposed to excluding it from the constructor.
         this.appService = appService;
      }
    }
    

    关于 Angular 如何注入东西以及它如何存储进来的东西,这是一种代码的浪费。通过注入 AppService,Angular 还神奇地为类存储了它,以便它可以作为 this.appService 使用。 我的典型版本是前缀为private readonly,因为......你为什么要改变你正在注入的东西?

    constructor(private readonly appService: AppService) { }
    

    没有任何前缀,它是公共的(而不是只读的),但上面的意思是它只对类可用(私有)并且不能覆盖它(只读)。这是一个偏好,但请注意构造函数的主体是空的; Angular 神奇地存储了它并留给我使用。 更多 C# 示例; C# 不会自动执行此类操作,您必须自己执行与该示例类似的操作:

    public class SomeClass {
      private readonly AppService _appService;
    
      public SomeClass(AppService appService) {
        _appService = appService;
      }
    }
    

    但同样,Angular 没有必要。

    最后,你的第三个例子:

    export class someClass {
      constructor(private storage: Storage) {}
      
      async getSomeVariable(): Promise<any> {
         // Mentioned as a parameter in the constructor 
        // but never set to anything or defined as a variable outside of the constructor?
         return await this.storage.get('someVariable');
      }
    }
    

    坦率地说,这是您第二个示例的延续。 Angular 已经提取(或创建了一个新实例)Storage 类供您的类使用(如果您不熟悉依赖注入和/或 Angular 的实现,请继续查看他们的 docs主题)。 然后它会自动将其保存为后台的storage 变量,这就是方法内容正在访问的内容。

    总的来说,我认为您对每种情况下的作用都有基本的了解,但在每种情况下,Angular 的特定实现都混淆了理解的水域,因此您永远无法确定。

    类似的事情反过来发生(同样,C#)。以 C# 对公共/私有 getter 和 setter 的简写:

    public string Something { get; set; }
    public string SomethingElse { get; private set; }
    // etc.
    

    漂亮的单线。 在 Angular(更准确地说,TypeScript)中,它们没有唯一的内衬,你必须写:

    private _something: string;
    public get something(): string { return this._something; }
    

    两行。从技术上讲,这也是 C# 在幕后为您所做的事情。

    private string _something;
    public string Something { 
      get { return _something; } 
      set { _something = value; }
    }
    

    以同样的方式,根据您的示例,Angular 会在后台隐藏这些额外的行并注入构造函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-21
      • 1970-01-01
      • 2014-09-21
      • 1970-01-01
      • 1970-01-01
      • 2016-12-24
      • 1970-01-01
      相关资源
      最近更新 更多