基本Java /构造函数/方法 [英] Basic Java / Constructor / Methods

查看:102
本文介绍了基本Java /构造函数/方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

向所有CodeProject成员致意,

i是Java的新手,我想问为什么我的Constructor和show()方法不起作用?

我在哪里?错了?







Greetings to all CodeProject members ,
i am new to Java ,and i would like to ask why my Constructor and show() method doesn²t work ?
Where is my mistake ?



import java.io.*;

 class Salarie
  {
   String name;
   String company;
   int sailary ;
  }

 public Sailary(String n, String e, int s)
  {
   name=n;
   company=e;
   sailary=s;
  }

 public void show()
  {
   System.out.println(nom+" "+company+" "+sailary); 
  }
  

public void main(String args[])
    {
      sailary sa=new sailary("martin","ETV",1500);
      p1.show();
    }

推荐答案

如果你想写一个 Java 程序然后你必须知道并使用正确的 Java 语法。

If you want to write a Java program then you must know and use the correct Java syntax.
import java.io.*;

class Salary
{
   String name;
   String company;
   int salary ;


 public Salary(String n, String e, int s)
  {
   name=n;
   company=e;
   salary=s;
  }

  public void show()
  {
   System.out.println(name+" "+company+" "+salary);
  }


  public static void main(String args[])
    {
      Salary sa = new Salary("martin","ETV",1500);
      sa.show();
    }

}


我认为你必须在开始编码之前阅读基本的java语法。很多很多可以在互联网上找到EBOOKS。特别是如何创建课程以及如何在课堂内创建方法等。

例子



class薪水

{

字符串名称;

字符串公司;

int salary; //通常工资不是int它应该是double或者浮动





//工资类的构造函数//没有返回类型,必须是班级名称

公共薪水(字符串a,字符串b,int c)

{

this.Name = a;

this.Company = b;

this.Salary = c;



}



public void show()

{

System.out.println(姓名+\ n+公司+\ n+工资);

}



public static void main(String [] args)

{



//将值传递给构造函数相同的数据参数的类型和数量必须相同



薪资s =新工资(Nuke。ABC,12000);

s.show();





}



}
i think you have to read the basic java syntax before starting the coding.there is lot of EBOOKS available in the internet.specially how to create the class and how to create the methid inside the class etc.
example

class Salary
{
String Name;
String Company;
int salary; // Normally salary is not a int it should be either double or float


// Constructor of Salary Class // no return type, must be Class name
public Salary (String a, String b, int c)
{
this.Name = a;
this.Company = b;
this.Salary =c;

}

public void show()
{
System.out.println(Name + "\n" + Company + "\n" + Salary);
}

public static void main(String [] args)
{

//passing the values into the constructor same data type and number of parameter must same

Salary s = new Salary("Nuke"."ABC",12000);
s.show();


}

}


主要是因为你拼写的一切都不同。您为类使用了 Salarie Sailary 用于构造函数, sailary 尝试实例化对象时。您还在 show 方法中拼错了其他位。在打字方面需要多加小心谨慎。
Largely because you spelled everything differently. You used Salarie for your class, Sailary for the constructor, and sailary when trying to instantiate an object. You also spelled other bits wrong in your show method. A bit more care in typing would go a long way.


这篇关于基本Java /构造函数/方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆