简体   繁体   English

布尔表达式如何在Java中工作?

[英]How boolean expression works in java?

 public class monstertestdrive {

public static void main(String[] args) {
    monster[] ma=new monster[3];
    ma[0]=new vampier();
    ma[1]=new monster();
    ma[2]=new dragon();
    for(int x=0;x<3;x++){
        ma[x].frighten(x);
      }
    }

}

class monster{
boolean frighten(int d){
    System.out.println("arrah");
    return false;
  }
}

class vampier extends monster{
boolean frighten(int x){
    System.out.println("a bite?");
    return true;
 } 
}

class dragon extends monster{
boolean frighten(int degree){
    System.out.println("breath fire");
    return true;
 }
}

Is the return type mentioned in frighten method serves any purpose? frighten方法中提到的返回类型有什么用吗? I asked this because i interchanged the return types but the output was same. 我问这个是因为我交换了返回类型,但是输出是相同的。 But when i changed the array object place ie 0,1 and 2 then i got a different output. 但是,当我更改数组对象的位置,即0,1和2时,我得到了不同的输出。

The boolean return and the integer parameters have no affect on your output because the methods make no use of them. 布尔返回整数参数对您的输出没有影响,因为这些方法没有使用它们。 You never use the return values from any of the methods to set any variables or determine the output. 您永远不会使用任何方法的返回值来设置任何变量或确定输出。 You also never use any of the passed in values to change anything within the method or output from the method. 您也永远不会使用任何传入的值来更改方法内的任何内容或方法的输出。 You may want to read up more on return types and methods and parameters first; 您可能想首先阅读有关返回类型,方法和参数的更多信息; it's basic knowledge that unless you plan to use the parameters or a return value, there's no reason to have either. 基本知识是,除非您打算使用参数或返回值,否则没有理由使用它们。 Once you understand that we can move on to why each element of the array outputs a different version of frighten. 一旦您了解了这一点,我们就可以继续讨论为什么数组的每个元素都输出不同版本的frighten。

You may want to read a bit first about concrete and abstract classes, polymorphism and inheritance. 您可能需要首先阅读有关具体和抽象类,多态性和继承的信息。

What you're looking at is an instance of dynamic (or late) method binding. 您正在查看的是动态(或较晚)方法绑定的实例。

Your parent CONCRETE class (monster) has a specific implementation of frighten(): 您的父级CONCRETE类(怪物)具有frighten()的特定实现:

boolean frighten(int d){
   System.out.println("arrah");
   return false;
}

This implementation of frighten has been written with a boolean return type and an integer parameter. frighten的此实现已使用布尔返回类型和整数参数编写。 When you extend this class, the child class inherits this method. 扩展此类时,子类将继承此方法。 Say I create another child class werewolf: 假设我创建了另一个子类别的狼人:

 class werewolf extends monster{}

At first glance this class has no methods or attributes associated with it; 乍一看,该类没有与之关联的方法或属性; but in reality, due to class inheritance it implicitly has the frighten method declared in its parent class. 但实际上,由于类继承,它隐式具有在其父类中声明的frighten方法。 This is what the compiler sees: 这是编译器看到的:

 class werewolf extends monster{
      boolean frighten(int d){ // Method **from** the parent class
      //This is implicitly built into the subclass due to inheritance
           System.out.println("arrah");
           return false;
      }
 }

However, these methods are not set in stone. 但是,这些方法并非一成不变。 Within the child class it is possible to override parent class methods. 在子类中,可以覆盖父类方法。 This is what you have done in your vampier and dragon subclasses. 这是您在吸血鬼和龙类中所做的。 This is an override because your methods have the same return type, same method name, and same parameters. 这是一个重写,因为您的方法具有相同的返回类型,相同的方法名称和相同的参数。 If you were to change any of these things it would be a different method altogether and would not be an override of the parent method; 如果要更改其中的任何一项,它将完全是另一种方法,并且不会替代父方法。 you should get into the habit of using the @Override directive which tells the compiler to check and make sure that you are actually following the right format for an override . 您应该养成使用@Override指令的习惯,该指令告诉编译器进行检查,并确保您实际上遵循正确的格式进行覆盖

 class dragon extends monster{
      @Override // This tells the compiler to check the parent for this method you are going to override and if you've followed the right format
      boolean frighten(int degree){
           System.out.println("breath fire");
           return true;
      }
 }

Dynamic/late method binding is the ability of a program to resolve references to their respective class implementations at runtime. 动态/后期方法绑定是程序在运行时解析对各自类实现的引用的能力。

All your subclasses are instances of the superclass they extend, so when you create an array of references to the superclass type, you can populate them with subclasses (a bit of a self promo here: Why can an array of references to an interface house classes that implement that interface? ). 您的所有子类都是它们扩展的超类的实例,因此,当创建对超类类型的引用的数组时,可以用子类填充它们(此处有点自我宣传: 为什么可以对接口内部类的引用数组实施该接口? )。

This is because subclasses have an ISA relationship with their parent class. 这是因为子类与其父类具有ISA关系。 However when you execute your main method: 但是,当您执行main方法时:

  for(int x=0;x<3;x++){
    ma[x].frighten(x);
  }

You haven't explicitly told the compiler, how do I pick which version of frighten() to use? 您尚未明确告诉编译器,如何选择要使用的frighten()版本? Well that's the great thing about polymorphism - You don't have to . 好吧,这就是多态性的妙处- 您不必如此 When the code executes, it's smart enough to look at the actual class that the reference is pointing to and, if the method is overridden properly, say HEY, there's a different local implementation of this method, let's execute that one and not the one in it's parent class. 当代码执行时,它足够聪明,可以查看引用所指向的实际类 ,如果方法被正确覆盖(例如,嘿),则此方法有不同的本地实现,让我们执行一个而不是一个这是父类。 However, if you change the return value or the parameters of the frighten() method in a child class in any way, it becomes not an override, and the executed method will default to the type of the reference (in this case the monster class because your reference array is one of monsters). 但是,如果您以任何方式更改子类中的返回值或frighten()方法的参数,它都不会被覆盖,并且执行的方法将默认为引用的类型(在这种情况下为Monster类)因为您的参考数组是怪兽之一)。

So this isn't an issue with how boolean works, but an issue of how to properly use overridden methods. 因此,这不是布尔值如何工作的问题,而是如何正确使用覆盖的方法的问题。 Hope this helped you out! 希望这对您有所帮助!

Yes, the return types do matter. 是的,返回类型很重要。 the boolean return type returns either true or false . boolean返回类型返回truefalse You can also make an int return type and return a number. 您还可以设置一个int返回类型并返回一个数字。 this is handy for many things 这在很多事情上都很方便

for example: 例如:

int sum(int x, int y)
{
    return x + y;
}

You're not noticing a difference because you're not doing anything with the return value 您没有注意到差异,因为您没有对返回值做任何事情

ma[x].frighten(x);

If your function has nothing meaningful to return, than you might as well make the return type void and return nothing at all 如果您的函数没有任何有意义的返回值,那么最好使返回类型为void并且什么也不返回

void frighten(int degree){
    System.out.println("breath fire");
}

我敢打赌在Monster的frighten方法中更改返回类型(即,从boolean更改为int,然后将返回值从false更改为0)以尝试不同的多态情况...然后获得不同的输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM