我有一个抽象类ship
。
from abc import ABC, abstractmethod
class ship(ABC):
def __init__(self):
...
@abstractmethod
def do_stuff(self,stuff,things):
pass
我有多个继承自它的类(destroyer
,cruiser
,patrol_boat
等等。)
class carrier(ship):
def __init__(self):
....
def do_stuff(self,stuff,things):
....
目前,如果我要添加,请说def do_more_stuff(self):
到ship
class ship(ABC):
def __init__(self):
...
@abstractmethod
def do_stuff(self,stuff,things):
pass
@abstractmethod
def do_more_stuff(self,stuff,things):
pass
在我将其重新输入控制台之前,更改不会影响任何子类。我该如何改变?
答案 0 :(得分:1)
如果你真的从头开始重新定义一个类,它就是一个不同的类;子类仍然继承旧版ship
。您不能只定义一个名为ship
的新类,并希望子类能够神奇地找到它。
通常情况下,如果您想在创建后使用ship
进行猴子补丁添加新方法,您可以执行以下操作:
def do_more_stuff(self,stuff,things):
pass
ship.do_more_stuff = do_more_stuff
但不幸的是,abstractmethod
are an explicit exception to this rule ABC
为using System;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
namespace OpenGLLine
{
static class Program
{
static OpenTK.GameWindow gw;
static int shaderProgram;
static int vertexInfo;
static int lineVertexBuffer;
static int vertexCount;
[STAThread]
static void Main()
{
gw = new OpenTK.GameWindow(800, 600, OpenTK.Graphics.GraphicsMode.Default, "Game", OpenTK.GameWindowFlags.FixedWindow);
Initialize();
gw.RenderFrame += Gw_RenderFrame;
gw.Closed += Gw_Closed;
gw.Run(60);
}
private static void Gw_Closed(object sender, EventArgs e)
{
CleanUp();
}
private static void Initialize()
{
int vshader = GL.CreateShader(ShaderType.VertexShader);
GL.ShaderSource(vshader, @"#version 130
in vec2 vPosition;
void main()
{
gl_Position = vec4(vPosition, -1.0, 1.0);
}");
GL.CompileShader(vshader);
int fshader = GL.CreateShader(ShaderType.FragmentShader);
GL.ShaderSource(fshader, @"#version 130
out vec4 fragColor;
void main()
{
fragColor = vec4(1.0, 1.0, 1.0, 1.0);
}");
GL.CompileShader(fshader);
shaderProgram = GL.CreateProgram();
GL.AttachShader(shaderProgram, vshader);
GL.AttachShader(shaderProgram, fshader);
GL.LinkProgram(shaderProgram);
GL.DetachShader(shaderProgram, vshader);
GL.DetachShader(shaderProgram, fshader);
GL.UseProgram(shaderProgram);
lineVertexBuffer = GL.GenBuffer();
Vector2[] lineVertices = { new Vector2(0, 0), new Vector2(.5f, .5f) };
vertexCount = lineVertices.Length;
GL.BindBuffer(BufferTarget.ArrayBuffer, lineVertexBuffer);
GL.BufferData(BufferTarget.ArrayBuffer, System.Runtime.InteropServices.Marshal.SizeOf(lineVertices[0]) * vertexCount,
lineVertices, BufferUsageHint.StreamDraw);
vertexInfo = GL.GenVertexArray();
GL.BindVertexArray(vertexInfo);
int locVPosition = GL.GetAttribLocation(shaderProgram, "vPosition");
GL.EnableVertexAttribArray(locVPosition);
GL.VertexAttribPointer(locVPosition, 2, VertexAttribPointerType.Float, false,
System.Runtime.InteropServices.Marshal.SizeOf(lineVertices[0]), 0);
GL.BindVertexArray(0);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.UseProgram(0);
}
static void CleanUp()
{
GL.DeleteProgram(shaderProgram);
GL.DeleteVertexArray(vertexInfo);
GL.DeleteBuffer(lineVertexBuffer);
}
private static void Gw_RenderFrame(object sender, OpenTK.FrameEventArgs e)
{
GL.ClearColor(Color4.Black);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.BindBuffer(BufferTarget.ArrayBuffer, lineVertexBuffer);
GL.UseProgram(shaderProgram);
GL.BindVertexArray(vertexInfo);
GL.DrawArrays(PrimitiveType.LineStrip, 0, vertexCount);
GL.BindVertexArray(0);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.UseProgram(0);
gw.SwapBuffers();
}
}
}
:
不支持动态地向类添加抽象方法,或者在创建方法或类时尝试修改方法或类的抽象状态。
您必须事先完全定义抽象基类,或者如果要将新抽象方法添加到基类中,请稍后重新定义整个类层次结构。