printf,说,getName setName [英] printf, saying, getName setName

查看:84
本文介绍了printf,说,getName setName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚看了一个YouTube教程,名为许多方法和实例".他编写了一个程序,您可以在其中输入一些内容,并说您的第一个gf是 _ ".但这太复杂了.首先是主要班级:

I just watched a youtube tutorial called "many methods and instances". He made a program in which you enter something and it says "your first gf was _". But it was way too overcomplicated. First is the main class:

import java.util.Scanner;

public class MethodsInstances2 {
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        MethodsInstances object = new MethodsInstances();
        System.out.println("Enter name of first gf here: ");
        String temp = input.nextLine();
        object.setName(temp);
        object.saying();
    }
}

下一个是它从以下对象创建对象的类:

Next is the class it makes an object from:

public class MethodsInstances {
    private String girlName;
    public void setName (String name){
        girlName=name;
    }
    public String getName (){
        return girlName;
    }
    public void saying(){
        System.out.printf("Your first gf was %s", getName());
    }
}

似乎太过复杂了,考虑到我仍然是Java的新手,所以标题是我所不了解的所有东西.这是我输入的内容,速度快了4倍:

It seemed WAY too overcomplicated, and the title is all of the stuff i didn't understand, considering I'm still a newbie at Java. Here's what I typed which took 4 times faster:

import java.util.Scanner;
public class programtest {
    public static void main(String args[]){
        Scanner test = new Scanner(System.in);
        String name;
        System.out.println("Enter the name of your first girfriend: ");
        name = test.next();
        System.out.println("Your first girlfriend was " + name);
    }
}

有人可以告诉我要做孩子所说的话,以及标题词是什么意思吗?

Can someone tell me the point of doing what the tut said to do, and what the title words mean?

非常感谢,丹

推荐答案

该教程名为许多方法和实例",从我的POV中非常简单地显示了如何创建类,实例化它并调用其方法.如果它打算以最简单的方式进行操作,则将其称为获取一些控制台输入并将其吐出".

The tutorial is named "many methods and instances" and from my POV shows very simply how to create a class, instantiate it and call its methods. It would have been called "get some console input and spit it back out" if it was meant to do things the easiest way possible.

视频教程的重点是不是创建一个过于复杂的程序,而是演示如何调用对象上的方法.

The point of the video tutorial was not to create an overly complicated program but rather to demonstrate how to call methods on objects.

您有一个名为MethodInstances的类(不是一个很好的示例名称,btw),您可以对其进行实例化,然后在其上调用方法以保存状态(gf名称)并取回并显示它. (我建议对示例进行重构以使其更有意义.因为这样的用法引起混乱.)

You have a class called MethodInstances (not a great example name, btw) which you instantiate and then call methods on it to save state (gf name) and the get it back and display it. (I would suggest refactoring the example to have it make more sense. The verbiage as it is causes confusion.)

尝试一下:

import java.util.Scanner;

public class ObjectInstanceMethodExample {
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        FirstGirlfriend firstGF = new FirstGirlfriend();
        System.out.println("Enter name of first gf here: ");
        String temp = input.nextLine();
        firstGF.setName(temp);
        firstGF.sayFirstGirlfriendName();
    }
}

class FirstGirlfriend {
    private String girlName;
    public void setName (String name){
        girlName=name;
    }
    public String getName (){
        return girlName;
    }
    public void sayFirstGirlfriendName(){
        System.out.printf("Your first gf was %s", getName());
    }
}

通过避免创建单独的类并改用局部变量来缩短该过程.它可以工作,更容易,但这不是本教程要教的内容.

You short circuited the process by avoiding creating a separate class and using a local variable instead. It works, it's easier, but that's not what the tutorial was trying to teach.

我建议在接触OOP或...拥抱OOP并运行它之前,为初学者找到更简单的示例.

I suggest finding even simpler examples for starters before getting into OOP stuff or... embracing OOP and running with it.

顺便说一句,您将永远不会以最简单的方式做事,从而无法成为一名程序员.最简单的方法似乎只是在短期内最简单的方法.任何简单但复杂的项目,都会以最简单的方式迅速变得难以为继.

Btw, you will not progress as a programmer by always doing things the easiest way. Easiest only seems easiest in the near term. Any less-than-trivial project with even a bit of complexity will quickly become untenable by doing it the easiest way.

这篇关于printf,说,getName setName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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