java和c++都是面向对象的设计,但是不同的是c++有多继承,而java则没有,但是java提供了接口这个概念,接口是一种特殊的类,多重继承的能力通过接口实现。

java在类层次上提出了包的概念,为的是减少命名冲突和扩大名字空间。

java和c++一样,都提供了限定访问权限的修饰符,private,public,protect,c++如果不写,则默认是privat权限,而java不写,则默认是friendly,相应的成分可以被所在包中的各类访问,如表所示。

类型 无修饰符

private

            </td>
            <td>
                <span style="font-family:'Microsoft YaHei';font-size:16px;">protec</span><span style="font-family:'Microsoft YaHei';font-size:16px;">t</span><span style="font-family:'Microsoft YaHei';font-size:16px;">e</span><span style="font-family:'Microsoft YaHei';font-size:16px;">d</span> 
            </td>
            <td>
                <span style="font-family:'Microsoft YaHei';font-size:16px;">public</span> 
            </td>
        </tr>
        <tr>
            <td>
                <span style="font-family:'Microsoft YaHei';font-size:16px;">同一类</span> 
            </td>
            <td>
                <span style="font-family:'Microsoft YaHei';font-size:16px;">是</span><span style="font-family:'Microsoft YaHei';font-size:16px;">&nbsp;</span> 
            </td>
            <td>
                <span style="font-family:'Microsoft YaHei';font-size:16px;">是</span> 
            </td>
            <td>
                <span style="font-family:'Microsoft YaHei';font-size:16px;">是</span><span style="font-family:'Microsoft YaHei';font-size:16px;"></span><br />
            </td>
            <td>
                <span style="font-family:'Microsoft YaHei';font-size:16px;">是</span><span style="font-family:'Microsoft YaHei';font-size:16px;"></span><br />
            </td>
        </tr>
        <tr>
            <td>
                <span style="font-family:'Microsoft YaHei';font-size:16px;">同一包中的子类</span> 
            </td>
            <td>
                <span style="font-family:'Microsoft YaHei';font-size:16px;">是</span> 
            </td>
            <td>
                <span style="font-family:'Microsoft YaHei';font-size:16px;">否</span> 
            </td>
            <td>
                <span style="font-family:'Microsoft YaHei';font-size:16px;">是</span><span style="font-family:'Microsoft YaHei';font-size:16px;"></span><br />
            </td>
            <td>
                <span style="font-family:'Microsoft YaHei';font-size:16px;">是</span><span style="font-family:'Microsoft YaHei';font-size:16px;"></span><br />
            </td>
        </tr>
        <tr>
            <td>
                <span style="font-family:'Microsoft YaHei';font-size:16px;">同一包中的非子类</span> 
            </td>
            <td>
                <span style="font-family:'Microsoft YaHei';font-size:16px;">是</span> 
            </td>
            <td>
                <span style="font-family:'Microsoft YaHei';font-size:16px;">否</span><span style="font-family:'Microsoft YaHei';font-size:16px;"></span><br />
            </td>
            <td>
                <span style="font-family:'Microsoft YaHei';font-size:16px;">是</span><span style="font-family:'Microsoft YaHei';font-size:16px;"></span><br />
            </td>
            <td>
                <span style="font-family:'Microsoft YaHei';font-size:16px;">是</span><span style="font-family:'Microsoft YaHei';font-size:16px;"></span><br />
            </td>
        </tr>
        <tr>
            <td>
                <span style="font-family:'Microsoft YaHei';font-size:16px;">不同包中的子类</span> 
            </td>
            <td>
                <span style="font-family:'Microsoft YaHei';font-size:16px;">否</span> 
            </td>
            <td>
                <span style="font-family:'Microsoft YaHei';font-size:16px;">否</span><span style="font-family:'Microsoft YaHei';font-size:16px;"></span><br />
            </td>
            <td>
                <span style="font-family:'Microsoft YaHei';font-size:16px;">是</span><span style="font-family:'Microsoft YaHei';font-size:16px;"></span><br />
            </td>
            <td>
                <span style="font-family:'Microsoft YaHei';font-size:16px;">是</span><span style="font-family:'Microsoft YaHei';font-size:16px;"></span><br />
            </td>
        </tr>
        <tr>
            <td>
                <span style="font-family:'Microsoft YaHei';font-size:16px;">不同包中的非子类</span> 
            </td>
            <td>
                <span style="font-family:'Microsoft YaHei';font-size:16px;">否</span> 
            </td>
            <td>
                <span style="font-family:'Microsoft YaHei';font-size:16px;">否</span><span style="font-family:'Microsoft YaHei';font-size:16px;"></span><br />
            </td>
            <td>
                <span style="font-family:'Microsoft YaHei';font-size:16px;">否</span><span style="font-family:'Microsoft YaHei';font-size:16px;"></span><br />
            </td>
            <td>
                <span style="font-family:'Microsoft YaHei';font-size:16px;">是</span><span style="font-family:'Microsoft YaHei';font-size:16px;"></span><br />
            </td>
        </tr>
    </tbody>
</table>

在引用方式上面,以helloworld为例,在c++中,静态成员函数是helloworld::fun()这样引用,而java则是helloworld.fun();

在对象引用成员变量的方法上一致。

Last modification:January 1st, 1970 at 08:00 am
如果觉得我的文章对你有用,请随意打赏: ☞已打赏列表

Leave a Comment