我如何... Java编码良好实践 - 一个例子 [英] How do I...Java coding good practice - an example

查看:87
本文介绍了我如何... Java编码良好实践 - 一个例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编写Java代码有一些困惑,我需要澄清一下:





  import  java.util.Scanner; 

public class NameAbbreviation {

private static String fullName;
private static 扫描程序nameInput;

public NameAbbreviation( String name){
NameAbbreviation.fullName =姓名;
}

public char getInitialLetterOfFirstName(){
char initialletterOfFirstName = fullName.charAt( 0 );
// System.out.println(initialletterOfFirstName);
return initialletterOfFirstName;
}

public String getInitialLetterOfLastName(){
int space = fullName.lastIndexOf( );
字符串 initialletterOfLastName = fullName.substring(空格+ 1
空格+ < span class =code-digit> 2
);
// System.out.println(initialletterOfLastName);
return initialletterOfLastName;
}

public static void main( String [] args){

nameInput = 新的扫描仪(System.in);
String name = nameInput.nextLine();
NameAbbreviation na = new NameAbbreviation(name);
System.out.println(na.getInitialLetterOfFirstName()+
+ na.getInitialLetterOfLastName());

}
}





我有类变量 fullName nameInput 的。如果我将 nameInput 设置为local,则显示:资源泄漏:nameInput永远不会关闭。所以我只是按照Eclipse的建议,把它变成一个字段。我应该怎么做?



我有两种方法 -

 getInitialLetterOfFirstName()

 getInitialLetterOfLastName()

,返回类型分别为char和String。我最终可以将返回类型设为void。如果我有无效或特定的退货类型,有什么优点或缺点?由于代码的工作方式,我需要遵循什么或者它取决于我如何编写代码?

两种方法

 getInitialLetterOfFirstName()

 getInitialLetterOfLastName()

没有任何参数/参数。我最终可以使用如下参数创建方法:



 getInitialLetterOfFirstName( String  name){
this .fullName = name; .......}





 getInitialLetterOfLastName(字符串名称){
.fullName = name; .......}





如果我用参数创建方法,我需要在实例化期间传递值宾语。所以我在这里避免它,并且更喜欢只有一次传递参数值。



是否有任何规则要遵循,比如是否使用参数制作方法,这里可以使用或不使用参数方法吗?



作为良好实践,代码性能问题等,我需要遵循什么??

解决方案

只需一个解决方案

  import  java.util。扫描器; 

public class NameAbbreviation {

private String fullName;

public void setFullName(字符串 fullName){
.fullName = fullName;
}

public char getInitialLetterOfFirstName(){
return fullName.charAt( 0 );
}

public String getInitialLetterOfLastName(){
return fullName.substring( 0 1 );
}

public void writeInitialLetterOfLastName(){
System.out.println( this .fullName.charAt( 0 ));
}

public static void main( String [] args){

扫描程序nameInput = new Scanner(System.in);
String name = nameInput.nextLine();
nameInput.close();

if (name.length()!= 0 ){
NameAbbreviation na = new NameAbbreviation();
na.setFullName(name);
System.out.println(na.getInitialLetterOfFirstName()+
+ na.getInitialLetterOfLastName());
na.writeInitialLetterOfLastName();
}
}
}






I have a few confusions about writing Java code, and I need some clarifications:


import java.util.Scanner;

public class NameAbbreviation {

private static String  fullName;
private static Scanner nameInput;

public NameAbbreviation(String name) {
    NameAbbreviation.fullName = name;
}

public char getInitialLetterOfFirstName() {
    char initialletterOfFirstName = fullName.charAt(0);
    //System.out.println(initialletterOfFirstName);
    return initialletterOfFirstName;
}

public String getInitialLetterOfLastName() {
    int space = fullName.lastIndexOf(" ");
    String initialletterOfLastName = fullName.substring(space + 1,
            space + 2);
    //System.out.println(initialletterOfLastName);
    return initialletterOfLastName;
}

public static void main(String[] args) {

    nameInput = new Scanner(System.in);
    String name = nameInput.nextLine();
    NameAbbreviation na = new NameAbbreviation(name);
    System.out.println(na.getInitialLetterOfFirstName() + " "
            + na.getInitialLetterOfLastName());

     }
}



I have class variables fullName and nameInput. If I make nameInput local, it shows: "Resource leak: nameInput is never closed." So I just follow the Eclipse suggestion and make it a field. What should I need to follow?

I have 2 methods -

getInitialLetterOfFirstName()

and

getInitialLetterOfLastName()

, the return types of which are "char" and "String" respectively. I could eventually make the return type as void. What are the advantages or disadvantages if I have void or specific return type? As both the way of code works, what do I need to follow or does it depend on how I am writing the code?
Both the methods

getInitialLetterOfFirstName()

and

getInitialLetterOfLastName()

don't have any parameters/arguments. I could eventually create the method with argument like:

getInitialLetterOfFirstName(String name) {
        this.fullName = name;    .......}



getInitialLetterOfLastName(String name) {
        this.fullName = name;    .......}



If I create the methods with arguments, I need to pass the value during the instantiation of the object. So I avoid it here, and prefer to have only once to pass the argument value.

Is there any rule to follow, like to make the methods with parameters or not, as here both are possible with or without argument methods?

What do I need to follow as good practice, code performance issues, etc.?

解决方案

Just one solution

import java.util.Scanner;

public class NameAbbreviation {

	private String fullName;

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	public char getInitialLetterOfFirstName() {
		return fullName.charAt(0);
	}

	public String getInitialLetterOfLastName() {
		return fullName.substring(0, 1);
	}

	public void writeInitialLetterOfLastName(){
		System.out.println(this.fullName.charAt(0));
	}
	
	public static void main(String[] args) {

		Scanner nameInput = new Scanner(System.in);
		String name = nameInput.nextLine();
		nameInput.close();

		if (name.length() != 0) {
			NameAbbreviation na = new NameAbbreviation();
			na.setFullName(name);
			System.out.println(na.getInitialLetterOfFirstName() +
					" " + na.getInitialLetterOfLastName());
			na.writeInitialLetterOfLastName();
		}
	}
}




这篇关于我如何... Java编码良好实践 - 一个例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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