我想创建一个扩展 CallableStatement 对象的子类。我想这样做,以便我可以覆盖 execute 和 executeQuery 方法来跟踪每个 SP 调用的一些指标。
目前我有如下代码:
Connection db = poolingDataSource.getConnection();
CallableStatement cstmt = db.prepareCall("{call pSampleStoredProc()}");
ResultSet rs = cstmt.executeQuery();
Run Code Online (Sandbox Code Playgroud)
其中 poolingDataSource 来自 apache commons dbcp 包。我的实现是使用 JDBC 连接到 MySQL 数据库中的。
目前,prepareCall 方法返回一个 com.mysql.jdbc.JDBC4CallableStatement。我希望能够更改它,以便它返回一个我自己的类,该类扩展了 JDBC4CallableStatement 但覆盖了 execute() 和 executeQuery() 方法。
有关执行此操作的最佳方法的任何想法?
我正在尝试了解Google Go的嵌入机制(作为子类化的替代方法).下面是一个简单的程序,总结了我的方法问题:
package main
import "fmt"
type Person struct {
Name string
}
func (p *Person) Talk() {
fmt.Println("Hi, my name is Person")
}
func (p *Person) TalkVia() {
fmt.Println("TalkVia ->")
p.Talk()
}
type Android struct {
Person
}
func (p *Android) Talk() {
fmt.Println("Hi, my name is Android")
}
func main() {
fmt.Println("Person")
p := new(Person)
p.Talk()
p.TalkVia()
fmt.Println("Android")
a := new(Android)
a.Talk()
a.TalkVia()
}
Run Code Online (Sandbox Code Playgroud)
输出是:
Person
Hi, my name is Person
TalkVia ->
Hi, my name is Person …Run Code Online (Sandbox Code Playgroud) 我创建了一个编辑框,它工作得很好,但在我为其添加自定义 WndProc 后,文本“我的编辑”不可见,并且单击时不会获得焦点。
HWND handle=CreateWindowExW(0,L"Edit",L"my edit",WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_CENTER | ES_MULTILINE | ES_AUTOVSCROLL,
0,0,200,200,window.handle,0,GetModuleHandle(NULL),0);
Run Code Online (Sandbox Code Playgroud)
直到这里它工作得很好在
我设置此窗口过程之后,编辑控件不再按预期工作
SetWindowLongPtr(handle,GWLP_WNDPROC,(LRESULT)staticWndProc);
LRESULT CALLBACK staticWndProc(HWND handle, UINT uMsg, WPARAM wParam, LPARAM lParam){
switch (uMsg){
case WM_LBUTTONDOWN:
std::wcout << handle << L" click\n"; //click event works
break;
default:
return DefWindowProcW(handle,uMsg,wParam,lParam);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我是否必须手动处理某些事件或更改我的构造风格标志?
我正在尝试编写一个类来处理按钮的创建及其内部消息。我希望保留类本身中包含的所有代码。这可能很简单,也可能不可能,因为我对 Winapi 和 C++ 比较陌生,更广泛地使用了 vb.net。我无法找到完成这种看似简单转换的示例。我确实找到了建议我使用备用 API 或使用 SetWindowLongPtr 的示例,我被告知 SetWindowSubclass 是更好的选择。我尝试的代码如下:
#pragma once
#include <iostream>
#include <Windows.h>
#include <Commctrl.h>
using namespace std;
class button {
public:
bool initialize();
buttton(int x, int y, int length, int width, LPCWSTR text, HWND parent, HINSTANCE hInstance); // This is the constructor
private:
LPCWSTR text = L"Button";
int height = 25;
int width = 100;
int x = 0;
int y = 0;
HWND parent;
HWND thisHandle;
HINSTANCE thisInstance;
bool initialized = false;
LRESULT …Run Code Online (Sandbox Code Playgroud) Java中的所有抽象类显然都是由具体类扩展的,并且不能自行实例化,我如何获得对扩展我的抽象类的类的引用?我的用例涉及反射,在我的父抽象类中,我想内省具体子类上的方法,但我无法弄清楚如何在不添加抽象方法强制子类返回其类的情况下获取对子类的引用。有任何想法吗?!
我想创建自己的控件,它将由几个UILabel和几个UITextField组成.问题是我不知道从哪里开始!我是否直接将UIControl子类化,然后创建我的子视图并将它们添加到init中的主视图中?或者我使用layoutSubviews?我是否需要覆盖drawRect:?
我习惯于创建"Controller"类来处理添加子视图,但如果我将UIControl子类化,那么我不确定要设置的覆盖方法!
我以前从未这样做过,所以我真的很感激一些指针!
干杯!
我为编辑和richedit做了这个确切的事情但是对于后者存在访问冲突,为什么?当tabctrl完成时也会发生同样的事情.我究竟做错了什么?我怎样才能让它发挥作用?
WNDPROC OriginalProc;
LRESULT CALLBACK MyProc(HWND h, UINT m, WPARAM w, LPARAM p)
{
return OriginalProc (h, m, w, p);// access violation for common controls
}
// elsewhere
HWND h = CreateWindow(....)
OriginalProc = (WNDPROC)SetWindowLong(h, GWL_WNDPROC, (LONG)MyProc);
Run Code Online (Sandbox Code Playgroud)
我正在使用vc ++ 6.
提前致谢.
正常覆盖将以这种方式工作:
class Fruit {
public:
string color();
};
string Fruit::color() {
return "Unkown";
};
class Apple : public Fruit {
public:
string color();
};
string Apple::color() {
return "Green";
};
Run Code Online (Sandbox Code Playgroud)
现在,你称之为:
Apple *apple = new Apple();
std::cout << apple->color();
Run Code Online (Sandbox Code Playgroud)
这将输出Green,这是正确的!但是,在以下情况下运行它(当然这只是一个例子):
Apple *apple = new Apple();
printHealthy(apple);
// Method printHealthy:
void printHealthy(Fruit *fruit) {
std::cout << fruit->color();
};
Run Code Online (Sandbox Code Playgroud)
这将输出Unkown,我可以理解,因为你将Apple转换为Fruit,从而'替换'它的方法.但我怎么还能了解它的真实颜色呢?
要求:
Apple--class.将会有更多Apple的东西,在旅途中分配.Apple类(例如Tomato,它们当然有不同的名称)是它的子类Fruit.Apple哪种颜色为 …我知道这是一个新手问题,但我是新手,所以这里是:
我希望在我的应用程序(按钮,标签等)中使用Chalkduster字体并且已经尝试了子类化UILabel来实现这一点.我在Default.h中有以下内容:
#import <UIKit/UIKit.h>
@interface Default : UILabel
{
UILabel *theLabel;
}
@property (nonatomic, strong) IBOutlet UILabel *theLabel;
@end
Run Code Online (Sandbox Code Playgroud)
这在我的.m中:
#import "Default.h"
@implementation Default
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
UIFont *custom = [[UIFont alloc] init];
custom = [UIFont fontWithName:@"Chalkduster" size:18];
self.font = custom;
NSLog(@"h");
}
return self;
}
@end
Run Code Online (Sandbox Code Playgroud)
当我在界面构建器中更改类并运行时,我没有看到Chalkduster字体.我很感激能够帮助我完成这项工作,因为我相信它会为我节省很多时间.干杯.
考虑一个矩形和一个正方形.如果我们将它们视为对象,很明显,正方形可以从矩形继承其大部分或全部属性,因为正方形是矩形的特殊情况.唯一的限制是正方形必须具有相似的边长.
看看这个非常基本的实现.
class Rectangle(object):
def __init__(self, x, y):
self.x = x
self.y = y
def get_area(self):
return self.x * self.y
class Square(Rectangle):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.x != self.y:
raise ValueError("x and y must be equal.")
Run Code Online (Sandbox Code Playgroud)
为简单起见,避免了类型检查.我使用args并且kwargs为了确保子类将是未来证明.但是,我不确定我是否正确行事.Rectangleclass' __init__可能会在将来添加更多参数.
问题是,如果x和y必须相等,为什么要首先要求它们?难道不应该只有一个吗?当前的继承方案有这个限制 - 用户被迫输入x和y.怎样才能解决这个问题.将x和y转换为关键字参数不是解决方案,因为不允许使用默认值(或None).
教Java的资源在是否可以覆盖父类的方法以及同时从子类中创建新方法的问题上似乎有冲突的答案。当我尝试以下操作时:
class ABC{
//Overridden Method
public void disp(){
System.out.println("disp() method of parent");
}
}
class DEF extends ABC{
//Overriding Method
public void disp(){
System.out.println("disp() method of child");
}
//new method in the subclass
public void newMethod(){
System.out.println("new method of child class");
}
public static void main(String args[]){
ABC obj = new ABC();
obj.disp();
ABC obj2 = new DEF();
obj2.disp();
obj2.newMethod();
}
}
Run Code Online (Sandbox Code Playgroud)
Java引发“找不到符号”错误。有什么我想念的吗?一次不能同时做这两项似乎很奇怪,但我似乎无法逃脱这一错误。
在C++中,"dynamic_cast"缓慢是一个众所周知的事实.我想到了以下简单的方法来了解层次结构中对象的类型.有人可以解释一下这是否比dynamic_cast慢?如果不是,那么为什么不是普遍的做法,因为速度的C++对C最差的缺点?
struct Base {
unsigned m_type;
Base(unsigned type): m_type(type) {}
Base(): m_type(0) {}
};
struct Derived1: Base {
Derived1(): Base(1) {}
Derived1(int type): Base(type) {}
};
struct Derived2: Base {
Derived2(): Base(2) {}
};
struct Derived3: Derived1 {
Derived3(): Derived1(3) {}
};
void my_func(Base * p) {
if (p - > m_type == 0) {}
else if (p - > m_type == 1) {}
else if (p - > m_type == 2) {}
else if (p - > m_type == …Run Code Online (Sandbox Code Playgroud) subclassing ×12
c++ ×4
java ×3
winapi ×3
oop ×2
overriding ×2
arguments ×1
c ×1
cocoa ×1
dynamic-cast ×1
edit-control ×1
editbox ×1
embedding ×1
extend ×1
go ×1
inheritance ×1
iphone ×1
jdbc ×1
mysql ×1
python ×1
richedit ×1
uicontrol ×1
uikit ×1