[转Iphone]关于self.用法的一些总结

 

MyClass.h

 
@interface MyClass : NSObject {
    MyObject * _myObject;
}
@property (nonatomic, retain) MyObject *myObject;
@end
 

MyClass.m

  @synthesize myObject = _myObject; 

OK, 你现在再试下, 如果你用self._myObject = aMyObject; 或者 myObject = aMyObject; 你会得到一个错误, 为什么呢, 这里就是和Obj-c的存取方法有关了. 说白了很简单 , 大家都知道, @property (nonatomic, retain) MyObject *myObject; 是为一个属性设置存取方法, 只是平时我们用的方法名和属性名是一样的,现在你把它写成不同的名字, 就会很清楚了. _myObject是属性本身, myObject是存取方法名.

 

 

 

 

assign, retain ,copy.

get方法是:

 
-(MyObject*)myObject{
    return _myObject;
}
 

Set方法是:

 
// assign 
-(void)setMyObject:(id)newValue{
    _myObject = newValue; 
}
// retain 
-(void)setMyObject:(id)newValue{
    if (_myObject != newValue) { 
        [_myObject release]; 
        _myObject = [newValue retain]; 
    }  
}
// copy 
-(void)setMyObject:(id)newValue{
    if (_myObject != newValue) { 
        [_myObject release]; 
        _myObject = [newValue copy]; 
    } 
}
 
示例:

    1.加self.:

 
        MyObject * aMyObject = [[MyObject alloc] init]; //aMyObject retainCount = 1;
        self.myObject = aMyObject; //myObject retainCount = 2;
        [aMyObject release];//myObject retainCount = 1;
 

    2. 不加self.:

 
       MyObject * aMyObject = [[MyObject alloc] init]; //aMyObject retainCount = 1;
        myObject = aMyObject; //myObject retainCount = 1;
        [aMyObject release];//对象己经被释放 
 

再看直接赋值的:

    3.加self.:

 

self.myObject= [[MyObject alloc] init]; //myObject retainCount = 2;
 

    4. 不加self.:

 
        myObject = [[MyObject alloc] init]; //myObject retainCount = 1;
 

 

import pygame import random import math import sys import os # 初始化pygame,设置环境变量避免Jupyter中的显示问题 os.environ['SDL_VIDEO_WINDOW_POS'] = "0,0" pygame.init() # 设置窗口尺寸 WIDTH, HEIGHT = 1000, 700 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Python烟花特效") # 颜色定义 BLACK = (0, 0, 0) WHITE = (255, 255, 255) COLORS = [ (255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255), (255, 165, 0), (255, 192, 203), (138, 43, 226) ] # 烟花粒子类 class Particle: def __init__(self, x, y, color): self.x = x self.y = y self.color = color self.radius = random.randint(1, 3) self.speed = random.uniform(2, 6) self.angle = random.uniform(0, math.pi * 2) self.vx = math.cos(self.angle) * self.speed self.vy = math.sin(self.angle) * self.speed self.gravity = 0.1 self.life = 100 self.alpha = 255 self.trail = [] self.trail_length = 15 def update(self): self.x += self.vx self.y += self.vy self.vy += self.gravity self.life -= 1 self.alpha = int(self.life * 2.55) # 添加轨迹点 self.trail.append((int(self.x), int(self.y))) if len(self.trail) > self.trail_length: self.trail.pop(0) def draw(self, surface): # 绘制轨迹 for i, pos in enumerate(self.trail): alpha = int(255 * (i / len(self.trail))) color_with_alpha = (*self.color, alpha) radius = max(1, int(self.radius * (i / len(self.trail)))) pygame.draw.circle(surface, color_with_alpha, pos, radius) # 绘制粒子 color_with_alpha = (*self.color, self.alpha) pygame.draw.circle(surface, color_with_alpha, (int(self.x), int(self.y)), self.radius) def is_dead(self): return self.life <= 0 or self.y > HEIGHT or self.x < 0 or self.x > WIDTH # 烟花类 class Firework: def __init__(self, x, y): self.x = x self.y = y self.color = random.choice(COLORS) self.y_velocity = random.uniform(-12, -8) self.gravity = 0.15 self.particles = [] self.exploded = False self.target_y = random.randint(50, HEIGHT // 2) self.speed = random.uniform(2, 4) self.trail = [] def update(self): if not self.exploded: self.y += self.y_velocity self.y_velocity += self.gravity # 添加轨迹 self.trail.append((int(self.x), int(self.y))) if len(self.trail) > 10: self.trail.pop(0) # 检查是否需要爆炸 if self.y_velocity >= 0: self.explode() else: # 更新所有粒子 for particle in self.particles[:]: particle.update() if particle.is_dead(): self.particles.remove(particle) def draw(self, surface): if not self.exploded: # 绘制轨迹 for i, pos in enumerate(self.trail): alpha = int(255 * (i / len(self.trail))) pygame.draw.circle(surface, (*self.color, alpha), pos, max(1, i//3)) # 绘制烟花主体 pygame.draw.circle(surface, self.color, (int(self.x), int(self.y)), 4) pygame.draw.circle(surface, WHITE, (int(self.x), int(self.y)), 2) else: # 绘制所有粒子 for particle in self.particles: particle.draw(surface) def explode(self): self.exploded = True particle_count = random.randint(80, 150) # 正确的变量名 shape = random.choice(["circle", "sphere", "ring", "heart"]) # 修正拼写错误:使用 particle_count 而不是 article_count for _ in range(particle_count): if shape == "circle": self.particles.append(Particle(self.x, self.y, self.color)) elif shape == "sphere": color = random.choice(COLORS) self.particles.append(Particle(self.x, self.y, color)) elif shape == "ring": angle = random.uniform(0, math.pi * 2) dist = random.uniform(20, 60) offset_x = math.cos(angle) * dist offset_y = math.sin(angle) * dist self.particles.append(Particle(self.x + offset_x, self.y + offset_y, self.color)) elif shape == "heart": # 心形烟花 t = random.uniform(0, math.pi * 2) x = 16 * (math.sin(t) ** 3) y = -(13 * math.cos(t) - 5 * math.cos(2*t) - 2 * math.cos(3*t) - math.cos(4*t)) scale = random.uniform(5, 10) self.particles.append(Particle(self.x + x * scale, self.y + y * scale, self.color)) def is_dead(self): return self.exploded and len(self.particles) == 0 # 创建半透明表面用于实现拖尾效果 def create_alpha_surface(width, height): surface = pygame.Surface((width, height), pygame.SRCALPHA) surface.fill((0, 0, 0, 25)) # 黑色带透明度 return surface # 主函数 def main(): # 在Jupyter中需要先初始化显示 from IPython.display import display, clear_output import ipywidgets as widgets clock = pygame.time.Clock() alpha_surface = create_alpha_surface(WIDTH, HEIGHT) fireworks = [] # 使用系统字体,确保在Jupyter中能正确显示 try: font = pygame.font.SysFont('Arial', 36) title_font = pygame.font.SysFont('Arial', 72, bold=True) except: font = pygame.font.Font(None, 36) title_font = pygame.font.Font(None, 72) # 创建输出小部件 out = widgets.Output() display(out) running = True while running: with out: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: running = False elif event.key == pygame.K_SPACE: # 手动发射烟花 for _ in range(5): fireworks.append(Firework(random.randint(100, WIDTH-100), HEIGHT)) elif event.type == pygame.MOUSEBUTTONDOWN: # 点击鼠标发射烟花 fireworks.append(Firework(event.pos[0], HEIGHT)) # 随机发射烟花 if random.random() < 0.05: fireworks.append(Firework(random.randint(100, WIDTH-100), HEIGHT)) # 应用半透明表面实现拖尾效果 screen.blit(alpha_surface, (0, 0)) # 更新和绘制所有烟花 for firework in fireworks[:]: firework.update() firework.draw(screen) if firework.is_dead(): fireworks.remove(firework) # 添加地面效果 pygame.draw.rect(screen, (30, 30, 50), (0, HEIGHT - 20, WIDTH, 20)) # 显示标题和说明 title = title_font.render("Python烟花特效", True, (200, 200, 255)) screen.blit(title, (WIDTH//2 - title.get_width()//2, 20)) instructions = [ "按空格键发射烟花", "点击鼠标发射烟花", "按ESC键退出" ] for i, text in enumerate(instructions): rendered = font.render(text, True, (180, 220, 255)) screen.blit(rendered, (20, 20 + i * 40)) # 显示烟花数量 count_text = font.render(f"烟花数量: {len(fireworks)}", True, (220, 220, 150)) screen.blit(count_text, (WIDTH - count_text.get_width() - 20, 20)) pygame.display.flip() clock.tick(60) # 清除之前的输出 clear_output(wait=True) pygame.quit() # 在Jupyter中运行 if __name__ == "__main__": main() 我用jupyter notebook运行的代码,但是出现中文文字显示问题
06-28
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值