标签: subclass

iOS中UIButton的简单子类化

我想我已经跟着示例代码到了这封信,但是下面给了我一个错误.

我想继承UIButton的子类并添加一些属性,但是我从一开始就失败了.

我创建了一个子类文件.这些是我的.h/.m's:

// damButton.h
#import <UIKit/UIKit.h>

@interface damButton : UIButton
{
    CGFloat _position;
}
@property (nonatomic) CGFloat position;
@end
Run Code Online (Sandbox Code Playgroud)

// damButton.m
#import "damButton.h"

@implementation damButton

@synthesize position = _position;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
@end
Run Code Online (Sandbox Code Playgroud)

在我的mainviewcontroller中,我导入了我的自定义按钮,但是当我使用该属性的内置getter和setter时,我收到一个错误:

//MainViewController.m
#import "damButton.h"

// then within a method...
damButton *b = [damButton buttonWithType:UIButtonTypeRoundedRect];
[b position:5.0];
Run Code Online (Sandbox Code Playgroud)

生成此错误: No visible @interface for 'damButton' declares the selector 'position:'

我不确定我在这里缺少什么,我几乎已经逐字复制了(我想).我只想使用内置的getter/setter(现在).

我错过了什么?

subclass uibutton ios

-1
推荐指数
1
解决办法
492
查看次数

如何访问抽象超类实例变量

所以我有两个班:PropertyHouses.Property是抽象的超类,Houses是它的子类.

这是代码 Property

public abstract class Property{
     String pCode;
     double value;
    int year;

    public Property(String pCode, double value , int year){
        this.pCode = pCode;
        this.value = value;
        this.year = year;
    }

        public Property(){
            pCode = "";
            value = 0;
            year = 0;
        }
    public abstract void depreciation();

    //Accessors
    private String getCode(){
        return pCode;
    }
    private double getValue(){
        return value;
    }
    private int getYear(){
        return year;
    }
    //Mutators
    private void setCode(String newCode){
        this.pCode …
Run Code Online (Sandbox Code Playgroud)

java subclass super abstract

-1
推荐指数
1
解决办法
2634
查看次数

使用isinstance方法的Python 3意外行为

我看到了一些我不希望的isinstance方法的奇怪行为.有人可以帮我确定为什么会这样吗?

我有一个模块sandbox.py,我在创建模块时用它来修改模块.我还有一个二叉树类binary_tree.py和一个BST类bst.py,它继承自二叉树实现并添加了树的排序约束.我也有一些在树上运行的实用方法,比如BFS,DFS等.

问题是:Bst类(BST节点)是Node的子类(通用二叉树节点).我的实用程序方法有一些检查以确保它们的参数是Node或其子类型的实例:

def bfs(n: Node, process=None):
    . . . 
    assert isinstance(n, Node)
    # print for debugging
    print("util.py:", isinstance(n, Node))
    . . . 
Run Code Online (Sandbox Code Playgroud)

在bfs方法中,断言通过以下调用,然后打印打印:

tree = Bst("A")
bfs(tree, lambda n: print(n.data, end=' ')) # Ignore the implementation, just know this enters the method
util.py: True
Run Code Online (Sandbox Code Playgroud)

正如所料.但是,在sandbox.py中,同一个调用打印为False:

from trees.binary_tree import Node
from trees.util import *
from trees.bst import Bst

print("sandbox.py:", isinstance(Bst, Node))
sandbox.py: False
Run Code Online (Sandbox Code Playgroud)

为什么isinstance在从不同位置调用时会返回两个不同的东西,即使这两个参数属于同一个类?

如果它相关,我的目录结构是这样的:

sandbox.py
trees/
    binary_tree.py
    bst.py
    util.py
Run Code Online (Sandbox Code Playgroud)

在bst.py中,Bst定义如下:

Bst(Node):
    . . . 
Run Code Online (Sandbox Code Playgroud)

python inheritance subclass python-3.x

-1
推荐指数
1
解决办法
52
查看次数

从超类C#访问子类方法

因此,我知道我实际上无法从其父级访问子类的方法和属性。但是我有一个程序可以将Vehicles存储在一个数组中(不是arraylist-它的作业)。然后将Vehicles初始化为从Vehicle类派生的飞机,船只或汽车对象。这些子类具有唯一的属性,我想知道如何去访问它们?

以下是一些(简化的)相关代码:

Vehicle[] vehicles = new Vehicle[20];

vehicles[0] = new Airplane();

// Setting an attribute of the superclass
vehicles[0].Make = "Boeing";

// Set an attribute from the Airplane class
vehicles[0].Engine = "Jet"; //(obviously this doesn't work)
Run Code Online (Sandbox Code Playgroud)

我该如何解决呢?我已经研究了几个小时,但是这个问题让我很困惑。

谢谢 :)

c# oop subclass superclass

-1
推荐指数
1
解决办法
62
查看次数

使用关键字问题的C++子类

说我有以下内容:

template<typename TemplateItem>
class TestA
{
    public:
        TemplateItem Item;

        void Function1(){;}
        void Function2(){;}
        void Function3(){;}
        void Function4(){;}
        //Etc etc number of functions is quite lengthy
};

template<typename TemplateItem>
class TestB : public TestA<TemplateItem>
{
    public:
        //How would I use the 'using' keyword to include everything from TestA
        //Without manually declaring it for 20 or more functions/variables individually
};
Run Code Online (Sandbox Code Playgroud)

对于那些不熟悉模板类继承问题的人:

http://www.parashift.com/c++-faq-lite/templates.html#faq-35.19

有些人似乎仍然认为没有这样的遗传问题,尽管上面的链接说明相反,但引用上面给出的网站:

template<typename T>
 class B {
 public:
   void f() { }  ? member of class B<T>
 };

 template<typename T>
 class …
Run Code Online (Sandbox Code Playgroud)

c++ subclass

-2
推荐指数
1
解决办法
452
查看次数

可以对UILocalNotification进行子类化并更改默认的"关闭"按钮文本和方法吗?

正在寻找一种方法来更改UILocalNotification"关闭"按钮文本/功能......

我发现从另一个对象访问/调用text/function 是不可能的,尽管子类化 UILocalNotification应该允许实现方法覆盖 ...更不用说创建一个访问器来获取/设置"关闭"按钮文本字段.

你们怎么看待这件事?Apple会怎么样?

有人试过吗?......

编辑:12/21/2011 12:01 PM

我问的问题涉及对oop的理解:延迟/早期绑定,动态方法查找,以及声明的类型与运行时类型字段和方法处理.

UILocalNotification的子类化确实有效......

UILocalNotificationExampleSubclass * example = [UILocalNotificationExampleSubclass init];

... 然而,设备创建一个对象,UILocalNotification而不是类型UILocalNotificationExampleSubclass.

我正在寻找对UILocalNotification.m文件方法的深入了解.

如果它没有自己的方法,那么什么对象(名称请)采用UILocalNotification的实例,使用它的字段,并显示我们在屏幕上看到的对象(名称)

overriding subclass objective-c uilocalnotification

-2
推荐指数
1
解决办法
1677
查看次数

如何使用共享基类为派生类重载operator <<?

我试图operator<<在几个子类中重载.我有一个名为Question的超类,它有一个枚举值类型和一个字符串问题.该类的子类是TextQuestion,ChoiceQuestion,BoolQuestion和ScaleQuestion.TextQuestion没有其他数据字段.ChoiceQuestion有一个字符串向量,用于存储多项选择的可能性.BoolQuestion没有其他数据字段.ScaleQuestion有两个int值,low_high_,用于比例.

class Question {
public:
    enum Type{TEXT, CHOICE, BOOL, SCALE};
    Question():
        type_(), question_() {}
    Question(Type type, std::string question):
        type_(type), question_(question) {}
    friend std::ostream& operator<<(std::ostream& out, const Question& q);
    virtual void print(std::ostream& out) const;
    virtual ~Question();
    private:
    Type type_;
    std::string question_;
};

class TextQuestion: public Question {
public:
    TextQuestion():
        Question() {}
    TextQuestion(Type type, std::string question):
        Question(type, question) {}
    void print(std::ostream& out) …
Run Code Online (Sandbox Code Playgroud)

c++ virtual inheritance operator-overloading subclass

-2
推荐指数
1
解决办法
2061
查看次数

创建一个空的派生类有什么意义?

我正在阅读(MSVC++ 2010)limits标题用于知识目的,并注意到这个小宝石:

template<class _Ty>
    class numeric_limits
        : public _Num_base
    {
        //...Insert min, max, lowest, epsilon,
        //round_error, denorm_min, infinity, quiet_Nan, and signaling_Nan
        //that return _Ty(0).
        //In other words, this class does "nothing" in a "safe" manner.*
    };
Run Code Online (Sandbox Code Playgroud)

紧随其后:

template<class _Ty>
    class numeric_limits<const _Ty>
        : public numeric_limits<_Ty>
    {   // numeric limits for const types
    };

template<class _Ty>
    class numeric_limits<volatile _Ty>
        : public numeric_limits<_Ty>
    {   // numeric limits for volatile types
    };

template<class _Ty>
    class numeric_limits<const volatile _Ty>
        : public numeric_limits<_Ty> …
Run Code Online (Sandbox Code Playgroud)

c++ subclass

-2
推荐指数
1
解决办法
102
查看次数

Java为什么不允许从子类构造函数设置受保护的最终字段?

为什么不允许从子类构造函数设置受保护的最终字段?

例:

class A {
    protected final boolean b;

    protected A() {
        b = false;
    }
}

class B extends A {
    public B() {
        super();
        b = true;
    }
}
Run Code Online (Sandbox Code Playgroud)

我认为在某些情况下会有意义,不是吗?

java constructor final protected subclass

-2
推荐指数
1
解决办法
369
查看次数

Java - 调用超类的构造函数失败

这是我设置的基本课程,用于计算股票收益.关注这里的"股票"构造函数:

public class Stock{
    private String symbol;
    private int totalShares;
    private double totalCost;

    public void Stock(String symbol){
        this.symbol = symbol;
        totalShares = 0;
        totalCost = 0.0;
    }

    public double getProfit(double currentPrice){
        double marketValue = totalShares * currentPrice;
        return marketValue - totalCost;
    }

    public void purchase(int shares, double pricePerShare){
        totalShares += shares;
        totalCost += shares*pricePerShare;
    }

    public int getTotalShares(){
        return totalShares;
    }
}
Run Code Online (Sandbox Code Playgroud)

我创建了一个名为DividendStock的子类,它应该计算股息收益.

public class DividendStock extends Stock{
    private double dividends;

    public DividendStock(String symbol){
        super(symbol);
        dividends = 0.0;
    }

    public …
Run Code Online (Sandbox Code Playgroud)

java constructor subclass superclass

-2
推荐指数
1
解决办法
48
查看次数

即使我没有任何循环,它仍然让我循环

我不知道出了什么问题,但它让我循环,即使我按下或输入 1 并且我希望它调用子类代理,但它没有,但是当我按下 2 时,它调用子类,这里出了什么问题我一直在5-7小时,我想我的眼睛已经睁不开了

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;

class Person 
{
protected String agentId = "20860132";
protected String password = "20020729" ;
protected String address;

public Person(String agentId,String password, String address) 
{
    this.agentId = agentId;
    this.password = password;
    this.address = address;
    Scanner input = new Scanner(System.in);
    System.out.println("[1]AGENT");
    System.out.println("[2]CUSTOMER");
    int choice = input.nextInt();
    

    //here is where the code loops it just keeps repeating this lines 
   "System.out.println("[1]AGENT"); System.out.println("[2]CUSTOMER");"

    if(choice == 1) 
    {
       Agent …
Run Code Online (Sandbox Code Playgroud)

java arrays oop subclass superclass

-3
推荐指数
1
解决办法
41
查看次数