构造函数错误:如何通过参数化构造函数设置这些属性?

发布于 2025-01-11 17:24:25 字数 1496 浏览 0 评论 0原文

1.如何通过参数化构造函数设置这些属性?

类文件

public class Product{
    private String Name;
    private String Color;
    private int Price;
    private int Stock;
    private int SKU;
    
  1. 我想通过参数化构造函数传递的setter来设置这些属性,但它没有设置这些属性< /strong>

    public void setName(字符串名称){
        this.Name=名称;
    }
    公共无效setColor(字符串颜色){
        这.颜色=颜色;
    }
    公共无效setPrice(int价格){
        如果(价格 <= 0){
            System.out.println("价格不能为0或负数");
        }
        else{this.Price=价格;
        }
    
    }
    公共无效setStock(int股票){
        如果(库存== 0){
            System.out.println("缺货");
        }
        别的{
            this.Stock=库存;
        }
    
    }
    公共无效setSKU(int SKU){
        如果(SKU<6){
            System.out.println("最大允许数量为6");
        }
        别的{
            这个.SKU=SKU;
        }
    }
    
  2. 这里我想通过 toString 方法向我展示属性

    公共字符串 toString(){
        return "\n产品名称: " + this.Name + "\n产品颜色: " + this.Color + "\n产品价格: " + this.Price + "\n产品库存: " + this.Stock + "\n产品 SKU: " + 这个.SKU;
    }
    
    产品(){}
    
    //参数构造器
    产品(字符串名称,字符串颜色,int 价格,int 库存,int SKU){
        System.out.println("运行");
        设置名称(名称);
        设置颜色(颜色);
        设置价格(价格);
        设置股票(股票);
        设置SKU(SKU);
        toString();
    }
    

    } 目标文件

  3. 为什么我无法通过构造函数设置这些属性?

    公共类可运行{ 公共静态无效主(字符串[] args){ 产品 p1 = 新产品("pc","灰色",5000,10,2);

     //这一行也给出错误,为什么?
            //p1.Product("pc","灰色",5000,10,2);
        }
    }
    

1. how can set these properties through parametrize constructor??

Class file

public class Product{
    private String Name;
    private String Color;
    private int Price;
    private int Stock;
    private int SKU;
    
  1. I want to set these properties thorough the setter passing by parametrize constructor but it did not set these properties

    public void setName(String Name){
        this.Name=Name;
    }
    public void setColor(String Color){
        this.Color=Color;
    }
    public void setPrice(int Price){
        if (Price <= 0){
            System.out.println("Price can't be 0 or negative");
        }
        else{this.Price=Price;
        }
    
    }
    public void setStock(int Stock){
        if(Stock == 0){
            System.out.println("Stock not avaible");
        }
        else{
            this.Stock=Stock;
        }
    
    }
    public void setSKU(int SKU){
        if(SKU<6){
            System.out.println("Manimum allow quntity is 6");
        }
        else{
            this.SKU=SKU;
        }
    }
    
  2. Here I want to show me properties through toString method

    public String toString(){
        return "\nProduct name: " + this.Name + "\nProduct color: " + this.Color + "\nProduct price: " + this.Price + "\nProduct stock: " + this.Stock + "\nProduct SKU: " + this.SKU;
    }
    
    Product(){}
    
    //parametriaz constractor
    Product(String Name,String Color,int Price,int Stock,int SKU){
        System.out.println("Run");
        setName(Name);
        setColor(Color);
        setPrice(Price);
        setStock(Stock);
        setSKU(SKU);
        toString();
    }
    

    }
    Object file

  3. Why I am unable to set these properties through constructor??

    public class Runnable{
    public static void main(String [] args){
    Product p1 = new Product("pc","Grey",5000,10,2);

            //This line also give error why??
            //p1.Product("pc","Grey",5000,10,2);
        }
    }
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染墨丶若流云 2025-01-18 17:24:25

按照惯例,字段名称采用大写字母和小写字母。

public class Product {
    private String name;
    private String color;
    private int price;
    private int stock;
    private int sku;

    Product() {
    }

    //Parameterized constructor
    Product(String name, String color, int price, int stock, int sku) {
        System.out.println("Run");
        this.setName(name);
        this.setColor(color);
        this.setPrice(price);
        this.setStock(stock);
        this.setSku(sku);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        if (price <= 0) {
            System.out.println("Price can't be 0 or negative");
        } else {
            this.price = price;
        }

    }

    public int getStock() {
        return stock;
    }

    public void setStock(int stock) {
        if(stock == 0){
            System.out.println("Stock not avaible");
        }
        else{
            this.stock=stock;
        }
    
    }

    public int getSku() {
        return sku;
    }

    public void setSku(int sku){
        if(sku<6){
            System.out.println("Manimum allow quntity is 6");
        }
        else{
            this.sku=sku;
        }
    }

    @Override
    public String toString() {
        return "Product [color=" + color + ", name=" + name + ", price=" + price + ", sku=" + sku + ", stock=" + stock
                + "]";
    }



    //Consume
    public static void main(String[] args) {

        System.out.println("using Constructor only");
        Product p = new Product("pc","Grey",5000,10,2);   // Right way to call contructor
        System.out.println(p); // Prints the object using toString()
        
        //OR
        System.out.println(new Product("pc","Grey",5000,10,2));

        //OR
        System.out.println("using Setters and getters only");
        Product p1 = new Product();
        p1.setName("pc");
        p1.setColor("Grey");
        p1.setPrice(5000);
        p1.setStock(10);
        p1.setSku(2);

        System.out.println(p1);
    }

}
❯ java Product.java
using Constructor only
Run
Manimum allow quntity is 6
Product [color=Grey, name=pc, price=5000, sku=0, stock=10]
Run
Manimum allow quntity is 6
Product [color=Grey, name=pc, price=5000, sku=0, stock=10]
using Setters and getters only
Manimum allow quntity is 6
Product [color=Grey, name=pc, price=5000, sku=0, stock=10]

By Convention, Field names are cameCased and lowerCase.

public class Product {
    private String name;
    private String color;
    private int price;
    private int stock;
    private int sku;

    Product() {
    }

    //Parameterized constructor
    Product(String name, String color, int price, int stock, int sku) {
        System.out.println("Run");
        this.setName(name);
        this.setColor(color);
        this.setPrice(price);
        this.setStock(stock);
        this.setSku(sku);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        if (price <= 0) {
            System.out.println("Price can't be 0 or negative");
        } else {
            this.price = price;
        }

    }

    public int getStock() {
        return stock;
    }

    public void setStock(int stock) {
        if(stock == 0){
            System.out.println("Stock not avaible");
        }
        else{
            this.stock=stock;
        }
    
    }

    public int getSku() {
        return sku;
    }

    public void setSku(int sku){
        if(sku<6){
            System.out.println("Manimum allow quntity is 6");
        }
        else{
            this.sku=sku;
        }
    }

    @Override
    public String toString() {
        return "Product [color=" + color + ", name=" + name + ", price=" + price + ", sku=" + sku + ", stock=" + stock
                + "]";
    }



    //Consume
    public static void main(String[] args) {

        System.out.println("using Constructor only");
        Product p = new Product("pc","Grey",5000,10,2);   // Right way to call contructor
        System.out.println(p); // Prints the object using toString()
        
        //OR
        System.out.println(new Product("pc","Grey",5000,10,2));

        //OR
        System.out.println("using Setters and getters only");
        Product p1 = new Product();
        p1.setName("pc");
        p1.setColor("Grey");
        p1.setPrice(5000);
        p1.setStock(10);
        p1.setSku(2);

        System.out.println(p1);
    }

}
❯ java Product.java
using Constructor only
Run
Manimum allow quntity is 6
Product [color=Grey, name=pc, price=5000, sku=0, stock=10]
Run
Manimum allow quntity is 6
Product [color=Grey, name=pc, price=5000, sku=0, stock=10]
using Setters and getters only
Manimum allow quntity is 6
Product [color=Grey, name=pc, price=5000, sku=0, stock=10]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文