PHP 中的抽象关键字

发布于 2024-08-13 04:15:28 字数 99 浏览 9 评论 0原文

嘿,我对 PHP 很有经验,但我不知道关键字抽象在面向对象编程时的作用。谁能用简单的英语解释它的用途?

在什么情况下我会使用abstract关键字?它如何改变类/接口?

Hey, I'm quite experienced with PHP but I have no idea what the keyword abstract does when it comes down to object orientated programming. Can anyone explain in plain english what it can be used for?

What situations would I use the abstract keyword in? How does it change the class/interface?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

叹沉浮 2024-08-20 04:15:28

(希望这足够简单 - 我认为我不能做得更好^^)

abstract 类无法实例化:您只能创建另一个继承的类来自abstract 类,并实例化该子类。

如果您将某些方法声明为抽象方法,则必须在子类中定义这些方法,以便该方法可以实例化。

(Hope this is simple enough -- I don't think I can do better ^^ )

An abstract class can not be instanciated : you can only create another class that inherits from the abstract class, and instanciate that child class.

And if you declare some methods as abstract, those must be defined in the child class, for that one to be instanciable.

z祗昰~ 2024-08-20 04:15:28

声明一个类抽象意味着它必须被子类化才能使用。抽象类不能被实例化。人们可以将其视为可能包含实现代码的扩展接口(而不是接口)。

通过声明方法抽象,可以强制子类实现该方法。

Declaring a class abstract means that it must be subclassed in order to be used. An abstract class can not be instantiated. One can see it as an extended interface that might include implementation code (as opposed to an interface).

By declaring a method abstract, you force the sub class to implement the method.

素手挽清风 2024-08-20 04:15:28

上面已经提到了定义,现在我将尝试给您举一个例子:

“抽象”确保您遵循特定的逻辑,例如门票的材料始终是“纸质”,或者信用卡必须始终有“代码”。
如果您在一家具有严格标准化的大公司工作,或者如果您想“强制”开发人员遵循特定的结构,那么这一点很重要,这样他们的代码就不会变得一团糟。

    abstract class ticket{

    public function material()
    {
        return 'Paper';
    }

}

abstract class creditcard{

    public function material()
    {
        return 'Plastic';
    }

    abstract function setCode(); // the ";" semicolon is important otherwise it will cause an error

}

class key extends ticket{

    public function getMaterial()
    {
        return parent::material();
    }
}

class anotherKey extends creditcard{

    public function setCode($code)
    {
        $this->code = $code;
    }
}

如果我们没有定义“setCode”方法,解析器将在“new anotherKey”上返回错误

The definition is mentioned above, now I will try to give you an example:

"abstract" ensures that you follow a specific logic, e.g. a ticket's material is ALWAYS "paper", or a creditcard must always have a "code".
This is important if you work in a big company which has strict standardisation or if you want to 'force' your developers to follow a specific structure, so their code won't end up in a mess.

    abstract class ticket{

    public function material()
    {
        return 'Paper';
    }

}

abstract class creditcard{

    public function material()
    {
        return 'Plastic';
    }

    abstract function setCode(); // the ";" semicolon is important otherwise it will cause an error

}

class key extends ticket{

    public function getMaterial()
    {
        return parent::material();
    }
}

class anotherKey extends creditcard{

    public function setCode($code)
    {
        $this->code = $code;
    }
}

If we do not define the "setCode" method the parser will return an error on "new anotherKey"

最好是你 2024-08-20 04:15:28

抽象类用于实际的一种模型关系。例如,这允许数据库驱动程序映射层次结构,其目的是提供一个公共基类,即实际驱动程序类的方法的签名。然后根据实际驱动程序类中预定的签名来执行实现。

这是代码示例

<?php
abstract class AbstrakteKlasse {
  public abstract function methode();
}

class ImplementierendeKlasse extends AbstrakteKlasse {
  public function methode() {
    print "ImplementierendeKlasse::methode() aufgerufen.\n";
  }
}

$objekt = new ImplementierendeKlasse;
$objekt->methode();
?>

Abstract classes are used to an actual a-kind-of-model relationship. This allows for example a database driver to map the hierarchy, in which aims to provide a common base class, the signatures for the methods of the actual driver classes. The implementation is then carried out in accordance with the predetermined signatures in the actual driver classes.

here is code example

<?php
abstract class AbstrakteKlasse {
  public abstract function methode();
}

class ImplementierendeKlasse extends AbstrakteKlasse {
  public function methode() {
    print "ImplementierendeKlasse::methode() aufgerufen.\n";
  }
}

$objekt = new ImplementierendeKlasse;
$objekt->methode();
?>
﹂绝世的画 2024-08-20 04:15:28

虽然您无法实例化抽象类,但您可以声明可供派生类使用的具体方法/属性/变量(在 C# 中,AFAIK)

class Program
    {
        static void Main(string[] args)
        {
            Dog a = new Dog();
           //concrete properties and methods in abstract base class 
          //are available to derived class
            a.Name = "SuperDog";
            a.EatFood();
            //And now calling Dog's method
            a.Speak();            
            Console.WriteLine(a.GetType());

        }
    }

    public abstract class Animal
    {
        public string Name { get; set; }
        public void EatFood()
        {
            Console.WriteLine("Eating..");
        }
    }

    public class Dog :Animal
    {
        public void Speak()
        {
            Console.WriteLine("Bow .. Bow");
        }
    }

Though you cannot instantiate an abstract class, you can declare concrete methods/properties/variables (in C#, AFAIK) which will be available to the derived class

class Program
    {
        static void Main(string[] args)
        {
            Dog a = new Dog();
           //concrete properties and methods in abstract base class 
          //are available to derived class
            a.Name = "SuperDog";
            a.EatFood();
            //And now calling Dog's method
            a.Speak();            
            Console.WriteLine(a.GetType());

        }
    }

    public abstract class Animal
    {
        public string Name { get; set; }
        public void EatFood()
        {
            Console.WriteLine("Eating..");
        }
    }

    public class Dog :Animal
    {
        public void Speak()
        {
            Console.WriteLine("Bow .. Bow");
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文