封装和抽象 [英] encapsulation and abstraction

查看:101
本文介绍了封装和抽象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是封装和抽象?

回答:

What is encapsulation and abstraction ?

ans:

print int Id; -> is encapsulation or abstraction 
public int GetId -> abstraction or encapsulation 
{
return Id;
}



这不是封装.这意味着我们最终允许使用公共成员函数访问其私有成员,但如何实现呢?



This is not encapsulation.means finally we are allowing to access its private members using public member functions, but how do we achieve it?

推荐答案

这两个概念和代码行完全没有意义;试图将概念分类为封装和抽象是错误的.谁告诉你他们可以分开?粗略地说,抽象是整个人类文化,数学,计算机科学,哲学甚至艺术中最普遍的类别之一.在计算中,它无处不在.并且您尝试完全错误地应用运算符或".

例如,Id是作为任何可能的整数值的位置的抽象,而int是作为可能是许多可能变量的通用类型的类型的抽象.

关于封装,该术语通常在面向对象编程的领域中使用,但是您甚至没有描述任何类(并且您的方法语法错误,无法编译).同样,封装"不能是方法的任何给定行的属性.这是一个概念.但是,是的,封装的一个概念是能够间接但仅间接访问私有成员的能力.这个概念涉及更多的内容,所以这个问题根本没有道理.

您为什么不先简单地正确理解基本概念,然后尝试以整体的方式彻底理解它们,而不是尝试将基本事物简化为琐碎的事情?请参阅:
http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29 [ ^ ],
http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29 [ ^ ],
/en.wikipedia.org/wiki/Object_directional_programming#features"target =" _ blank"title ="新窗口> ^ ].

—SA
The attempt to get mapping between those two concepts and code lines is totally pointless; and an attempt to classify concepts into encapsulation and abstraction is just wrong. Who told you they could be separable? Roughly speaking, abstraction is one of the most universal categories in the whole human culture, mathematics, computer science, philosophy and even art; in computing, it is everywhere. And you are trying to apply the operator "or", totally incorrectly.

For example, Id is an abstraction as the location of any possible integer values, and int is an abstraction as the type which could be a common type of many possible variables.

As to the encapsulation, this term is usually used in the domain of object-oriented programming, but you did not even depict any class (and your syntax for a method is wrong, would not compile). Again, "encapsulation" cannot be a property of any given line of a method; this is a concept. But yes, one concept of encapsulation is the ability to access a private member indirectly, but only indirectly. There is a lot more involved in this concept, so this question makes no sense at all.

Why wouldn''t you simply right about the basic concepts first and try to understand them thoroughly, and in the holistic manner, not trying to reduce essential things to trivial? Please see:
http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29[^],
http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29[^],
http://en.wikipedia.org/wiki/Object_oriented_programming#features[^].

—SA


可以说,封装通常是继承层次结构和对该类的访问修饰符.

在继承中使用抽象来拥有一个您不能拥有其实例的类.它通常被使用,因此您拥有一个包含最重要信息的基础"类.子类也必须覆盖抽象类.

例如,假设我们要创建一个包含有关商店中顾客和工人信息的系统.然后我们可以制作一个这样的类:

well to say it very generally encapsulation is the inherit hierarchy and the access modifiers to the class.

abstraction is used in inherit to have a class you cant have a instance of. its generally used so you have a "base" class with the most important info. also an abstract class MUST be override by an child class.

for instance lets assume we wanna make a system containing informations about customers and workers in a store. then we can make a class like this:

public abstract class Person
{
    public string firstname { get; protected set; }
    public string lastname { get; protected set; }
    public int phonenumber { get; protected set; }

    public Person(string _firstname, string _lastname, int _phonenumber)
    {
        firstname = _firstname;
        lastname = _lastname;
        phonenumber = _phonenumber;
    }
}



请注意,此类是抽象的,不能具有自己的实例,并且还将充当我们的超类".

接下来,我们创建一个从"Person"继承的客户,这样客户将至少具有与人相同的功能.



note that this class is abstract and can not have its own instance and will also act as our "super-class".

next up we create a customer inherit from "Person" so a customer will have at least the same functionality as a person.

public class Customer : Person
{
    public string customerID { get; protected set; }

    public Customer(string _customerID, string _firstname, string _lastname, int _phonenumber)
        : base(_firstname, _lastname, _phonenumber)
    {
        customerID = _customerID;
    }
}



请注意,此处我们使用"base"关键字来表示我们使用超类"来包含这些项目.

接下来,我们使员工类也从人员类继承.



note here that we''re using the "base" keyword to say we use the "superclass" to have these items.

next we make a employee class also inherit from the person class.

public class Employee : Person
{
    public int worktime { get; protected set; }

    public Employee(int _worktime, string _firstname, string _lastname, int _phonenumber)
        : base(_firstname, _lastname, _phonenumber)
    {
        worktime = _worktime;
    }
}



现在到了多态的魔力.您现在可以在您的main方法(或集合/数据库类)中列出人员列表,并在列表中同时包含员工和客户,如下所示.



now to the magic of polymorphism. you can now in you main method (or a collection/database class) make a list with persons and have both employees and customers in that list as seen below.

static void Main(string[] args)
{
    List<Person> persons = new List<Person>() { new Employee(36, "claus", "dawnson", 12345678), new Customer("ge2tt64mg", "martin", "smith", 78901234) };
}



正如您在上面看到的,我们在人员列表中同时添加了一个雇员和一个客户,这基本上就是多态性的优点.

如果我们扩展小程序,使其还具有一些抽象和虚拟方法,这些方法将在继承中被覆盖,那么我们将发现可以很好地利用我们的多态性.完整示例如下:



as you can see above we add both a employee and a customer in a Person list and that is basically the good thing about polymorphism.

if we expand our small program to also have some abstract and virtual methods that will be override in the inherit we''ll get to a point where we will find really good use of our polymorphism. full sample here below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Person> persons = new List<Person>() { new Employee(36, "claus", "dawnson", 12345678), new Customer("ge2tt64mg", "martin", "smith", 78901234) };
            persons.OrderBy(p => p.lastname).ThenBy(p => p.firstname);
            foreach (Person p in persons)
            {
                Console.WriteLine(p.ToString());
            }
            Console.ReadKey();
        }
    }


    public abstract class Person
    {
        public string firstname { get; protected set; }
        public string lastname { get; protected set; }
        public int phonenumber { get; protected set; }

        public Person(string _firstname, string _lastname, int _phonenumber)
        {
            firstname = _firstname;
            lastname = _lastname;
            phonenumber = _phonenumber;
        }

        public abstract override string ToString();
        public virtual string GetCustomerID()
        {
            return null;
        }
    }

    public class Customer : Person
    {
        public string customerID { get; protected set; }

        public Customer(string _customerID, string _firstname, string _lastname, int _phonenumber)
            : base(_firstname, _lastname, _phonenumber)
        {
            customerID = _customerID;
        }

        public override string ToString()
        {
            return firstname + " " + lastname + " " + phonenumber + " " + customerID;
        }

        public override string GetCustomerID()
        {
            return customerID;
        }
    }

    public class Employee : Person
    {
        public int worktime { get; protected set; }

        public Employee(int _worktime, string _firstname, string _lastname, int _phonenumber)
            : base(_firstname, _lastname, _phonenumber)
        {
            worktime = _worktime;
        }

        public override string ToString()
        {
            return firstname + " " + lastname + " " + phonenumber + " " + worktime;
        }
    }
}



如您所见,我们添加了一个抽象的tostring方法,该方法必须在子类中被覆盖.您可以根据需要添加任意数量的抽象或虚拟.我必须重写所有我的所有子类,其中不需要重写但可以抽象的所有子类.

您可以在互联网上以及此站点上找到更多有关此的更多信息.我也建议您研究一下接口,因为有时它们也可以派上用场(请参阅我的一个老问题



as you can see we have added a abstract tostring method that MUST be override in the child classes. You can add as many as you want either abstract or virtual. abstract have to be override i all child classes where virtual is not needed to be override but it can.

you can find A LOT more about this all over the internet and here on this site. Also i will recommend you to look at interfaces since they some times also can come in handy (see one of my old questions Parse classes to interface that can inherit[^])

lastly, thanks for reading and i hope you find this useful and sorry for my crappy English (danish is my native language). fell free to ask if you have any questions.

- Jackie

PS. the best way to learn it is to try it out by you self, try to make some simple databases, for example try to make a small library/store selling pc parts etc.


看这个:
封装VS抽象 [ http://www.tutorialspoint.com/java/java_abstraction.htm [ http://www.tutorialspoint.com/java/java_encapsulation.htm [
Take a look at this:
Encapsulation VS Abstraction[^]

These are Java but they could help
http://www.tutorialspoint.com/java/java_abstraction.htm[^]
http://www.tutorialspoint.com/java/java_encapsulation.htm[^]


这篇关于封装和抽象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆