标签: constructor

'Order'不包含带0参数的构造函数

我得到'Order'不包含带0参数的构造函数.所以根据这个错误,我知道它在我的公共课程中.我在俯瞰什么?谢谢!

public class Order
{
    public int QuantityOrdered { get; set; }
    public double TotalPrice;
    public const double PRICEEACH = 19.95;

    virtual public double totalPrice
    {
        set
        {
            TotalPrice = QuantityOrdered * PRICEEACH;
        }
    }
}

public class ShippedOrder : Order
{
    public const double SHIPPINGFEE = 4.00; 
    public override double totalPrice
    {
        set
        {
            totalPrice = base.TotalPrice + SHIPPINGFEE;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# constructor arguments

-4
推荐指数
1
解决办法
1652
查看次数

为什么我们需要在C++中使用构造函数,我们可以通过函数的帮助(数据初始化或访问私有数据成员)来做同样的事情

请给出一个完美的例子.在下面的程序中,为什么我们需要构造函数,而我们可以通过函数做同样的事情.(假设我们通过main()传递一些值)

#include<iostream>
using namespace std;
class name {
private:
int roll_num;
char grade;
public:
void set_roll(int data);      /* function to get the values of roll num and grade */
void set_grade(char grade_1);
void display(void);
.
.
name(int ..., char ....)      /* constructor for the same thing as function doing */
Run Code Online (Sandbox Code Playgroud)

c++ constructor

-4
推荐指数
1
解决办法
3181
查看次数

为什么要调用超级构造函数?为什么我在打印到屏幕时出错?

我正在学习java编程.我尝试运行以下程序时收到编译错误.

public class Animal {
    public Animal() {
        System.out.println(“Making an Animal”);
    }
}

public class Hippo extends Animal {
    public Hippo() {
        System.out.println(“Making a Hippo”);
    }
}

public class TestHippo {
    public static void main (String[] args) {
         System.out.println(“Starting...”);
         Hippo h = new Hippo();
    }
}
Run Code Online (Sandbox Code Playgroud)

根据我的理解,如果这个程序运行没有错误,它将打印

开始...

制作河马

但在"Head First Java"一书中,他们提到了该程序的输出

开始...

制作动物

制作河马

这个输出怎么可能?我没有调用超类构造函数,但是如何在这里打印"制作动物".

任何人都可以解释这背后的逻辑吗?

如果我们调用子类构造函数,它的超类constrtuctor是否也被执行?

还有为什么我得到编译错误,我使用在线IDE来测试基本的java程序.

错误详情

编译错误注释(0)stdin copy标准输入为空编译信息Main.java:3:错误:非法字符:\ 8220 System.out.println(?制作动物?); ^ Main.java:3:错误:';' 期望System.out.println(?制作动物?); ^ Main.java:3:错误:非法字符:\ 8221 System.out.println(?制作动物?); ^ Main.java:8:错误:非法字符:\ 8220 System.out.println(?制作河马?); ^ Main.java:8:错误:';' 期待System.out.println(?制作河马?); ^ Main.java:8:错误:非法字符:\ …

java inheritance constructor

-4
推荐指数
1
解决办法
186
查看次数

为什么我不能在c ++中将函数求值作为构造函数参数传递?

我想知道为什么我不能将函数评估作为构造函数参数传递,即:

A a(fstream(argv[1]))
Run Code Online (Sandbox Code Playgroud)

如果我试着打电话a.dump(),我会收到错误.

如果我使用

fsrteam fin(argv[1]);
A a(fin);
Run Code Online (Sandbox Code Playgroud)

这很好用.

构造函数声明为

A(std::fstream &file)
Run Code Online (Sandbox Code Playgroud)

c++ constructor

-4
推荐指数
1
解决办法
101
查看次数

C++没有看到复制构造函数

我正在为我的项目开发一个小矢量类,基本上我不能从任何类型的数字构造一个矢量.即使它们都是不同的(例如Vector3(float,int,unsigned)).但是,我遇到了一个小问题,我迷失在这些模板及其顺序中.现在我的算术运算符无法正常工作.

这是vector类的代码:

#include "Defines.hpp" // Typedefs for the fundamental types...
#include "Math/Base.hpp" // Various numerical functions...

template<class T, class Enable = void>
class Vector3;

template<class T>
class Vector3<T, typename std::enable_if<std::is_arithmetic<T>::value >::type>
{
public:
    Vector3()
        : X(0), Y(0), Z(0)
    {

    }

//    explicit Vector3(T s)
//        : X(s), Y(s), Z(s)
//    {
//
//    }

    template <class U, typename std::enable_if<std::is_arithmetic<U>::value>::type* = nullptr>
    explicit Vector3(U s)
        : X(getBounded<T>(s)), Y(getBounded<T>(s)), Z(getBounded<T>(s))
    {

    }

//    explicit Vector3(T x, T y, T z)
//        : …
Run Code Online (Sandbox Code Playgroud)

c++ templates constructor c++11

-4
推荐指数
1
解决办法
130
查看次数

PHP将新对象创建到数组中

我创建了一个包含三个属性的Magazine类,我希望在创建对象时将其直接存储到数组中.现在,我认为我能做的只是直接在数组中创建新对象,但它似乎不起作用?

<?php

class Magazine {

  public $id;
  public $name;
  public $price;

  public function __contruct($id, $name, $price) {
    $this->id =     $id;
    $this->name =   $name;
    $this->price =  $price;
  }

}

$magazines = array(
  new Magazine("svg_aftbld","Stavanger Aftenblad", 200),
  new Magazine("tsk_ubld","Teknisk Ukeblad", 300),
  new Magazine("se_ho","Se og H&oslash;r", 100),
  new Magazine("fam","Familien", 150)
);

print_r($magazines);

?>
Run Code Online (Sandbox Code Playgroud)

这是上面代码的输出:

Array ( [0] => Magazine Object ( [id] => [name] => [price] => ) [1] => Magazine Object ( [id] => [name] => [price] => ) [2] => Magazine …
Run Code Online (Sandbox Code Playgroud)

php arrays oop constructor

-4
推荐指数
1
解决办法
63
查看次数

是否可以使用void返回类型的构造函数?

我想知道是否可以使用void返回类型的构造函数,如下例所示.

例如

class A
{
    void A(){}  //where A is constructor and for which return type is void

    public static void main(string arg[]){
        A a = new A();
    }
}
Run Code Online (Sandbox Code Playgroud)

java constructor

-4
推荐指数
1
解决办法
535
查看次数

在集合更新时,Refs在构造函数中使用?

我有一些服务的构造函数:

public Ctor(List<Items> items)
{
    _items = items;
}

public void Work()
{
    if(_items.Count() > 5) 
    //do some work
}
Run Code Online (Sandbox Code Playgroud)

Work方法将被逐个调用.所以,如果在我改变的对象之外items- 会发生什么?会_items.Count()改变吗?

因为在这个当前版本我使用这个:

public void Work()
{
     if(StaticClass.Items.Count() > 5)
    //do some work
}
Run Code Online (Sandbox Code Playgroud)

StaticClass.Items更新时,外面的项目集合改变.所以,如果我这样做:

 var service = new Constructor(StaticClass.Items);
Run Code Online (Sandbox Code Playgroud)

然后我更新StaticClass.Items元素 -

我可以_items.Count()Work方法调用时(通过某个计时器)使用实际值,还是应该使用ref关键字?

c# constructor

-4
推荐指数
1
解决办法
59
查看次数

java构造函数:this(.)

为什么输出为"021"?为什么有"0"和"1"(因为"我"得到"2"为什么它变成"1")?

public class C {
       protected int i;
       public C(int i){
               this(i,i);
               System.out.print(this.i);
               this.i=i;
}
      public C(int i, int j) {
               System.out.print(this.i);
               this.i=i+j;
}
       public C(){
              this(1);
              System.out.print(i);
}
      public static void main(String[] args) {
             C c=new C();
}}
Run Code Online (Sandbox Code Playgroud)

java constructor

-4
推荐指数
1
解决办法
69
查看次数

如果我有一个调用其他实例之一的重载函数,它是否被认为是一个递归函数?

这是假设,但我有一个带有两个重载构造函数的类 - 其中没有一个是默认的构造函数.如果我从另一个调用一个构造函数,它会递归吗?例:

class Example
{
     Example(const int integer)
     {
          //Constructor Code Here
     }

     Example(argument)
     {
          Example object(68);
          //Rest of constructor code
     }
};
Run Code Online (Sandbox Code Playgroud)

c++ recursion constructor class function-calls

-4
推荐指数
1
解决办法
97
查看次数

标签 统计

constructor ×10

c++ ×4

java ×3

c# ×2

arguments ×1

arrays ×1

c++11 ×1

class ×1

function-calls ×1

inheritance ×1

oop ×1

php ×1

recursion ×1

templates ×1