python中功能内的功能,如何在外部调用?类内,方法内的方法呢?在外部又是怎样调用到的?

6516℃ COLLEEN

python中功能内的功能,如何在外部调用?类内,方法内的方法呢?在外部又是怎样调用到的?

如何在一个类中调用另一个类的方法

java类有两种方法一种是类方法就是用static修饰的,一种是实例方法,就是没有static修饰的方法。类方法可以同时类名.方法名的方式调用。而实例方法必须先生存类的实例在通过实例.方法名的方式调用。例如:

public class MethodCall

{

public static void main(String[] args)

{

Test.sayStatic();

Test test = new Test();

test.sayInstance();

}

}

class Test

{

public static void sayStatic()

{

System.out.println("这是一个静态方法。");

}

public void sayInstance()

{

System.out.println("这是一个实例方法。");

}

}

Python 父目录或者同级目录下的模块怎么调用??

(1)主程序与模块程序在同一目录下:

如下面程序结构:

`-- src

|-- mod1.py

`-- test1.py

若在程序test1.py中导入模块mod1, 则直接使用import mod1或from mod1 import *;

(2)主程序所在目录是模块所在目录的父(或祖辈)目录

如下面程序结构:

`-- src

|-- mod1.py

|-- mod2

| `-- mod2.py

`-- test1.py

若在程序test1.py中导入模块mod2, 需要在mod2文件夹中建立空文件__init__.py文件(也可以在该文件中自定义输出模块接口); 然后使用 from mod2.mod2 import * 或import mod2.mod2.

(3)主程序导入上层目录中模块或其他目录(平级)下的模块

如下面程序结构:

`-- src

|-- mod1.py

|-- mod2

| `-- mod2.py

|-- sub

| `-- test2.py

`-- test1.py

若在程序test2.py中导入模块mod1和mod2。首先需要在mod2下建立__init__.py文件(同(2)),src下不必建立该文件。然后调用方式如下:

下面程序执行方式均在程序文件所在目录下执行,如test2.py是在cd sub;之后执行python test2.py

而test1.py是在cd src;之后执行python test1.py; 不保证在src目录下执行python sub/test2.py成功。

import sys

sys.path.append("..")

import mod1

import mod2.mod2

python 中open()的用法?

r表示只读,b表示二进制

与此对应的是w表示可写,t表示文本方式打开。

再增加一些官方的解释:

>>> print file.__doc__

file(name[, mode[, buffering]]) -> file object

Open a file. The mode can be 'r', 'w' or 'a' for reading (default),

writing or appending. The file will be created if it doesn't exist

when opened for writing or appending; it will be truncated when

opened for writing. Add a 'b' to the mode for binary files.

Add a '+' to the mode to allow simultaneous reading and writing.

If the buffering argument is given, 0 means unbuffered, 1 means line

buffered, and larger numbers specify the buffer size. The preferred way

to open a file is with the builtin open() function.

Add a 'U' to mode to open the file for input with universal newline

support. Any line ending in the input file will be seen as a '\n'

in Python. Also, a file so opened gains the attribute 'newlines';

the value for this attribute is one of None (no newline read yet),

'\r', '\n', '\r\n' or a tuple containing all the newline types seen.

内部类可以访问外部类的属性和方法吗

1.使用static可以声明一个内部类, 可以直接在外部调用 class Outer{ // 定义外部类 private static String info = "hello world" ; // 定义外部类的私有属性 static class Inner{ // 使用static定义内部类为外部类 public void print(){ /

TAG: 功能 方法