我正在为MySQL用PHP语言编写一个数据库交互层。但我认为这是一个一般性的OOP问题(参见最后一行)。
我有一个基本的dbTables类。它有一个
public static function getBy($method='name', $value) {
// Gets flat table array of db Tables matching by $method == $value
// later could implement some lookup tables.
$allowed = array('nam
我有一个关于使用init的实践的问题。如果我有一个具有多个子类的父类,并且它们在init函数中都有相同的内容,那么最好将该内容放在父init类中,然后简单地这样做:
public function init(){
parent::init();
}
这似乎是合乎逻辑的,但我读到的每一个地方,不幸的是zend特定的,他们说把父类中的init方法留空以避免复杂性。
有人能澄清一下吗?
起初我很困惑为什么构造函数中的这两个方法调用都能工作,但现在我想我明白了。扩展类继承父类的方法,就好像它们是在类本身中声明的一样,并且方法存在于父类中,因此两者都应该可以工作。
现在,我想知道是否有调用方法的首选方法(即最佳实践)(通过parent或this),以及这些方法是否真的是执行相同代码的相同方法,或者在使用其中一种方法时是否有任何注意事项。
抱歉,我可能想多了。
abstract class Animal {
function get_species() {
echo "test";
}
}
class Dog extends
我理解这个关键字用于调用特定类中的当前构造函数。但我有另一种情况,我需要更多地了解它。下一段代码显示了这个和超级关键字的用法:
public class Stack {
public void push()
{
/* do something */
}
public boolean full()
{
/* do something */
}
}
另一类是:
public class ProtectedStack extends Stack {
public void push()
{
我不希望任何人重写我的构造函数试图使用私有方法,但是它给了我错误--如何停止重写构造函数
class bc {
int a;
bc() {
System.out.println("hi this is construvtor ");
a = 10;
System.out.println("the value of a=" + a);
}
}
class dc extends bc {
int b;
dc() {
a = 20;
System.out.println("derived dc
如果您有一个名为" User“的类和另一个扩展了"User”的名为" Admin“的类,并且您希望Admin继承User的所有属性和方法,例如,__construct方法除外。
class User {
private $name;
function __construct($name) {
$this->name = $name;
}
}
和
class Admin extends User {
private $authorization;
function __construct($name,$authorization) {
$this->nam
我知道,在Java和C#类中,字段并不是过多的(但是方法是这样的)。我知道这个事实,但我不明白这种实施的原因是什么。这是不可预测和明显的。为什么我们有这样的OOP实现?
public class Flight {
int seats = 150;
}
public class CargoFlight extends Flight {
int seats = 12;
}
CargoFlight f1 = new CargoFlight();
System.out.println(f1.seats);
Flight f2 = new CargoFlight();
Syste
我想我知道super打算覆盖从父类继承的相同方法。但是,在下面的消息传递代码示例中,我不知道super应该在那里做什么:
class WellBehavedFooCatcher
def respond_to?(method_name)
if method_name.to_s[0,3] == "foo"
true
else
super(method_name)
end
end
end
那么,super在上面的代码中做了什么呢?
提前谢谢你!
这可能在某个地方得到了答案,但我不知道要搜索什么。假设你有以下内容……
超类Animal.java
public class Animal {
public String noise = "squeak";
public String toString() { return noise; }
}
子类Lion.java
public class Lion extends Animal {
public String noise = "ROAR!!";
public String toString() { return noise; }
}
主类R
我有以下尝试使用this和super的代码片段。
class SuperClass
{
public final int x=10;
public final String s="super";
public String notOverridden()
{
return "Inside super";
}
public String overrriden()
{
return "Inside super";
}
}
final class Su
在执行此操作时,我会得到以下错误:
The return type is incompatible with MouseAdapter.mouseClicked(MouseEvent)
类:
public class MyMouseAdapter extends MouseAdapter
{
public MyMouseAdapter()
{
// TODO Auto-generated constructor stub
}
@Override
public String mouseClicked(MouseEvent e)
{
当类扩展另一个类时,子类构造函数调用超类构造函数。
在这个过程中,创建了两个对象,子类和超类。我的问题是,在子类对象的生命周期中,这个超类对象会发生什么?
它是否会被垃圾收集,因为引用是子类的?
class A
{
public A()
{
System.out.println("super class constructor");
}
}
class B extends A
{
public B()
{
// super(); default added
System.out.pr
代码隐藏:
class A implements Cloneable
{
int i, j;
A(int i, int j)
{
this.i = i;
this.j = j;
}
A()
{
}
}
class B extends A
{
int l, m;
B()
{
}
B(int l, int m)
{
this.l = l;
this.m = m;
}
public static void
我有两个类,Employee和Department。
Department是Employee的子类。
代码如下:
public class Employee {
public void getEmployeeDetails(String name , String age) {
System.out.println("Employee.getEmployeeDetails()");
}
public void print() {
System.out.println("Employee.print()")
谁能给我解释一下这次执行的结果?我不知道为什么要调用每个方法。以及我们如何区分真实类型和表观类型。谢谢您:)
public class JavaApplication15 {
public static void main(String[] args) {
A a = new A();
B b= new B();
A ab = new B();
b.f(a);
b.f(b);
b.f(ab);
}
}
public class A {
private final String id=
在Java类和对象中,我们使用"this“关键字来引用类中的当前对象。在某种意义上,我相信“这个”实际上是返回自己的对象。
示例:
class Lion
{
public void Test()
{
System.out.println(this); //prints itself (A Lion object)
}
}
在超类和子类的场景中。我认为"super“关键字会返回超类的对象。不过,这次我似乎弄错了:
示例:
class Parent
{
public Parent(){
}
}
class Child e
我目前正在用编写python的模板方法模式。
我想知道是否有任何方法可以保护基类中的一些函数,以致子类不会被覆盖?如下所示,来自子类的_primitive_operation_3覆盖了来自基类的相同函数。
import abc
class AbstractClass(metaclass=abc.ABCMeta):
"""
Define abstract primitive operations that concrete subclasses define
to implement steps of an algorithm.
Impl
一些新手的问题,我有一个这样的模型:
from django.db import models
class SomeCommons(object):
# some fields here
# ...
class Meta:
abstract=True
class SomeDjangoModels(SomeCommons,models.Model):
pass
是否与以下模式相同:
from django.db import models
class SomeModels(models.Model):
# some fields here
#
代码1:
class Tyre
{
public void front() throws RuntimeException
{
System.out.println("Tire");
}
public void front(int a)
{
System.out.println("Radial Tire with int in tyre");
}
public void front(long a)
{
System.out.println(
我有这样的家长班和孩子班:
class Parent {
public String value = "Parent";
public String getValue() {
return value;
}
}
class Child extends Parent {
public String value = "Child";
public String getValue() {
return value;
}
}
public class Test {