简体   繁体   English

我不明白这段代码

[英]i dont understand this code

I read Hello Android book and I don't understand some parts of the code. 我读了Hello Android书,我不明白代码的某些部分。

public class PuzzleView extends View {
    private static final String TAG = "Sudoku" ;
    private final Game game;
    public PuzzleView(Context context) {
    super(context);
    this.game = (Game) context;
    setFocusable(true);
    setFocusableInTouchMode(true);
  }
 // ...
}

private float width; // width of one tile
private float height; // height of one tile
private int selX; // X index of selection
private int selY; // Y index of selection
private final Rect selRect = new Rect();
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    width = w / 9f;
    height = h / 9f;
    getRect(selX, selY, selRect);
    Log.d(TAG, "onSizeChanged: width " + width + ", height "+ height);
    super.onSizeChanged(w, h, oldw, oldh);
 }

super(context); 超级(上下文); in this code, what does it mean and what does it do? 在这段代码中,它意味着什么,它做了什么?

this.game = (Game) context; this.game =(游戏)上下文; why we wrote this? 为什么我们写这个? what does it do? 它有什么作用?

The Android site says that onSizeChanged() function is used for: "This is called during layout when the size of this view has changed" This meant that if to rotate the phone, this function causes the view of program to display true. Android网站说onSizeChanged()函数用于:“在布局时调用此视图的大小时更改”这意味着如果要旋转手机,此功能会使程序视图显示为true。 this is true? 这是真的?

getRect(selX,selY,selRect); getRect(selX,selY,selRect); what does it mean and what does it do? 它是什么意思,它做了什么?

Please help me. 请帮我。 Cheers. 干杯。

For your first question, Subclasses can call super class constructors with a call to super() 对于你的第一个问题,Subclasses可以通过调用super()来调用超类构造函数

ie

super(context);

calls the super class constructor: 调用超类构造函数:

public View(Context context)

In your second question, this.game = (Game) context; 在你的第二个问题中, this.game = (Game) context; does 2 things. 做两件事。 Firstly, it casts context to class Game and then it assigns it to the PuzzleView 's game field ( private final Game game; ). 首先,它将上下文转换为类Game ,然后将其分配给PuzzleView的游戏领域( private final Game game; )。

getRect is declared at the end of the PuzzleView code . getRectPuzzleView代码的末尾声明。 It changes the variable rect passed to it. 它改变传递给它的变量rect

   private void getRect(int x, int y, Rect rect) {
      rect.set((int) (x * width), (int) (y * height), (int) (x
            * width + width), (int) (y * height + height));

ATaylor has already addressed onSizeChanged . ATaylor已经解决了onSizeChanged Basically, the code overrides the inherited method, and then 'hooks' in additional functionality, before calling the super method. 基本上,代码会覆盖继承的方法,然后在调用super方法之前“挂钩”其他功能。 This is standard practice for overriding virtual methods (ie polymorphic behaviour). 这是覆盖虚拟方法(即多态行为)的标准做法。

As was already stated: 如前所述:

super(context);

will call the function of the same name of the parent class. 将调用父类的同名函数。

Assuming you have this polymorphism: Class Animal 假设你有这种多态性:Class Animal

void MakeNoise() {
    System.out.println("Generic Noise");
}

Class Dog extends Animal Class Dog延伸动物

void MakeNoise() {
    super();
    System.out.println("Woof");
}

When you're calling MakeNoise from a Dog Object, you'll have two outputs. 当您从Dog对象调用MakeNoise时,您将有两个输出。

Generic Noise (called by 'super') and 'Woof'. 通用噪音(由'超级'调用)和'Woof'。

this.game => Most certainly, you need to access the drawing context in that class, and for this you require a context of type 'Game' (I'm not familiar with Android, which is why I'm not sure, what type 'Game' is, but it seems to be compatible to context. this.game =>当然,你需要访问该类中的绘图上下文,为此你需要一个类型为'Game'的上下文(我不熟悉Android,这就是为什么我不确定,是什么类型'游戏'是,但它似乎与上下文兼容。

Whenever 'this.game' is accessed from that class, it will access the originally passed context and thereby draw on the surface of the device. 每当从该类访问'this.game'时,它将访问最初传递的上下文,从而绘制在设备的表面上。

And yes, onSizeChanged would be fired, whenever the size of the view is changed. 是的,只要视图的大小发生变化,就会触发onSizeChanged。

Regarding the getRect: No clue, actually, since I'm lacking the prototype or function declaration. 关于getRect:实际上没有任何线索,因为我缺乏原型或函数声明。 But from the looks of it, it will take arbitrary values (since the passed arguments have not been initialized, as far as I can see), and construct a 'Rect' structure from it (X/Y to W/Z) 但从它的外观来看,它将采用任意值(因为传递的参数尚未初始化,据我所知),并从它构造一个'Rect'结构(X / Y到W / Z)

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

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