我正在学习Java,在书中写的是#34;如果第一个语句不是对父构造函数的调用,Java编译器会自动插入对无参数构造函数super()的调用。"
但是对于我来说它是不完整的,完整的将是"如果第一个语句不是对父构造函数的调用,或者调用,那么Java编译器会自动插入对无参数构造函数super()的调用。此()" this()表示类中有或没有参数的任何构造函数,我是对的吗?
我的意思是,在第一种情况下,编译器确实会将super()添加为第一个语句,但在第二种情况下,对于带有参数的构造函数则不会:
案例1:
public class MyClass{
public MyClass() {}
}
编译后,变为:
public class MyClass {
public MyClass() {super();}
}
案例2:
public class MyClass{
public MyClass() {}
public MyClass(int num) { this(); }
}
编译后,变为:
public class MyClass{
public MyClass() {super();}
public MyClass(int num) { this(); }
}
答案 0 :(得分:3)
你是对的,调用同一个类的另一个构造函数,例如div
也是完全有效的。您可以使用import pygame
from pygame.locals import*
#initialise pygame
pygame.init()
WHITE = (255,255,255)
#counts which sprite you should be on when running
#create screen
screen_width = 160
screen_height = 144
screen_multiplier = 4
screen = pygame.display.set_mode(((screen_width*screen_multiplier), (screen_height*screen_multiplier)))
pygame.display.set_caption('Pokemon Blood Red')
#Sprite stuff
sprite = pygame.image.load('player_east_still.png')
#Reform the sprite
sprite = pygame.transform.scale(sprite, (10*screen_multiplier, 14*screen_multiplier))
sprite.set_colorkey(WHITE)
#############################################################################
class Player(pygame.sprite.Sprite):
def __init__(self):
super(Player, self).__init__()
self.player_sprite = pygame.Surface((10*screen_multiplier, 14*screen_multiplier))
self.player_sprite.fill(WHITE)
self.rect = self.player_sprite.get_rect()
self.rect.x = 200
self.rect.y = 200
class Blocks(pygame.sprite.Sprite):
def __init__(self):
super(Blocks, self).__init__()
self.trees = pygame.Surface((10*screen_multiplier, 14*screen_multiplier))
self.trees.fill(WHITE)
self.rect = self.trees.get_rect()
self.rect.x = 25
self.rect.y = 25
player = Player()
blocks = Blocks()
pygame.init()
amount_caught = 0
place = 1
catch1 = {'pokemon':'none',
'hp':0,
'attack':0,
'defence':0,
'sp_attack':0,
'sp_defence':0,}
background = 1
def area_load():
global background
if background == 1:
background = pygame.image.load('neuory_town.png').convert()
background = pygame.transform.scale(background, (160*screen_multiplier, 144*screen_multiplier))
area_load()
(x) = 160*0.45
(y) = 144*0.45
def caught():
if amount_caught == 0:
pass
#catch1 values are equal to wild one's
#Mainloop
crashed = False
while not crashed:
x_change = 0
y_change = 0
#Different buttons
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
pygame.quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -3*screen_multiplier
player.rect.x += x_change
screen.blit(player.player_sprite, (player.rect.x, player.rect.y))
hits = pygame.sprite.spritecollide(player, blockades, False)
if hits:
print ('Collision!')
x_change = 0
player.rect.x = 10*screen_multiplier + blocks.rect.x
sprite = pygame.image.load('player_west_still.png')
elif event.key == pygame.K_RIGHT:
x_change = 3*screen_multiplier
player.rect.x += x_change
screen.blit(player.player_sprite, (player.rect.x, player.rect.y))
hits = pygame.sprite.spritecollide(player, blockades, False)
if hits:
print ('Collision!')
x_change = 0
player.rect.x = blocks.rect.x - 10*screen_multiplier
sprite = pygame.image.load('player_east_still.png')
elif event.key == pygame.K_UP:
y_change = -3*screen_multiplier
player.rect.y += y_change
screen.blit(player.player_sprite, (player.rect.x, player.rect.y))
hits = pygame.sprite.spritecollide(player, blockades, False)
if hits:
print ('Collision!')
y_change = 0
player.rect.y = 14*screen_multiplier + blocks.rect.x
sprite = pygame.image.load('player_north_still.png')
elif event.key == pygame.K_DOWN:
y_change = 3*screen_multiplier
player.rect.y += y_change
screen.blit(player.player_sprite, (player.rect.x, player.rect.y))
hits = pygame.sprite.spritecollide(player, blockades, False)
if hits:
print ('Collision!')
y_change = 0
player.rect.y = blocks.rect.x - 14*screen_multiplier
sprite = pygame.image.load('player_south_still.png')
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
x_change = 0
elif event.key == pygame.K_RIGHT:
x_change = 0
elif event.key == pygame.K_UP:
y_change = 0
elif event.key == pygame.K_DOWN:
y_change = 0
x += x_change
y += y_change
player.rect.x += x_change
player.rect.y += y_change
blockades = pygame.sprite.Group()
blockades.add(blocks)
#Check for collisions
hits = pygame.sprite.spritecollide(player, blockades, False)
#Draw everything
sprite = pygame.transform.scale(sprite, (10*screen_multiplier, 14*screen_multiplier))
sprite.set_colorkey(WHITE)
screen.blit(player.player_sprite, (player.rect.x, player.rect.y))
screen.blit(blocks.trees, (blocks.rect.x, blocks.rect.y))
screen.blit(background,(0,0))
screen.blit(sprite,(player.rect.x,player.rect.y))
pygame.display.flip()
pygame.quit()
工具查看Java编译器发出的字节码:
this()
javap
构造函数委托给$ javap -c ../MyClass.class
Compiled from "MyClass.java"
public class MyClass {
public MyClass();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public MyClass(int);
Code:
0: aload_0
1: invokespecial #2 // Method "<init>":()V
4: return
}
,后者又调用默认的MyClass(int)
构造函数。