小编khe*_*ood的帖子

超级python 2.7

我试图了解如何super在python中使用

class people:   
 name = ''  
 age = 0  
  __weight = 0  

 def __init__(self,n,a,w):  
    self.name = n  
    self.age = a  
    self.__weight = w  
def speak(self):  
    print("%s is speaking: I am %d years old" %(self.name,self.age))  


class student(people):  
 grade = ''  
 def __init__(self,n,a,w,g):  
    #people.__init__(self,n,a,w)  
    super(student,self).__init__(self,n,a,w)
    self.grade = g  

 def speak(self):  
    print("%s is speaking: I am %d years old,and I am in grade %d"%(self.name,self.age,self.grade))  


s = student('ken',20,60,3)  
s.speak()
Run Code Online (Sandbox Code Playgroud)

上面的代码出现以下错误:

---------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-147-9da355910141> in <module>()
     10 
     11 …
Run Code Online (Sandbox Code Playgroud)

python subprocess python-2.7 python-3.x

6
推荐指数
1
解决办法
2万
查看次数

如何避免Java Generics扩展Comparable接口中未经检查的强制转换?

为什么(T)这个通用接口需要不安全的转换?如果T与自身相当,也就是ExtendedComparable<super of T>那些也意味着ExtendedComparable<T>,那么为什么需要ExtendedComparable<T>将类型擦除转换为T?

/* @param <T> T must be comparable to itself or any of its superclass
 * (comparables are consumers, thus acc. to the PECS principle 
 * = producer-extends,consumer-super we use the bounded wildcard type "super")
 */   
public interface ExtendedComparable<T extends ExtendedComparable<? super T>> {
    Comparator<? super T> getComparator();
    default boolean greaterThen(T toCompare) {
        return getComparator().compare((T) this, toCompare) > 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

java generics casting type-conversion type-erasure

6
推荐指数
1
解决办法
213
查看次数

Java/Apache POI - 如何更改/设置特定单元格中的字体大小?

我想更改工作表中单元格中填充的确定数据的字体大小。假设该单元格是第 26 列的第 2 行。以范围方式来说,这将是:Z2。字体大小为11。例如,我想将字体大小更改为72。

我怎样才能做到这一点?

java apache-poi

6
推荐指数
1
解决办法
2万
查看次数

一个 If 语句中的两个 Walrus 运算符

是否有在 1 个 if 语句中包含两个海象运算符的正确方法?

if (three:= i%3==0) and (five:= i%5 ==0):
    arr.append("FizzBuzz")
elif three:
    arr.append("Fizz")
elif five:
    arr.append("Buzz")
else:
    arr.append(str(i-1))
Run Code Online (Sandbox Code Playgroud)

该示例适用于threefive将“未定义”。

python python-3.x walrus-operator

6
推荐指数
1
解决办法
1465
查看次数

我如何摆脱这些(未经检查的电话)警告?

warning: [unchecked] unchecked call to setCellValueFactory(Callback<CellDataFeatures<S,T>,ObservableValue<T>>) as a member of the raw type TableColumn column1.setCellValueFactory(new PropertyValueFactory<String, State>("name"));   where S,T are type-variables:
    S extends Object declared in class TableColumn
    T extends Object declared in class TableColumn
Run Code Online (Sandbox Code Playgroud)

码:

column1.setCellValueFactory(new PropertyValueFactory<>("name"));
Run Code Online (Sandbox Code Playgroud)
warning: [unchecked] unchecked call to add(E) as a member of the raw type List
            transitionTable.getColumns().add(column1);
  where E is a type-variable:
    E extends Object declared in interface List
Run Code Online (Sandbox Code Playgroud)

码:

transitionTable.getColumns().add(column1);
Run Code Online (Sandbox Code Playgroud)
warning: [unchecked] unchecked call to setAll(Collection<? extends E>) as a member of the …
Run Code Online (Sandbox Code Playgroud)

java generics warnings javafx unchecked

5
推荐指数
1
解决办法
5508
查看次数

Python3 NameError:未为已定义的@staticmethod 定义名称“方法”

我正在尝试在 python3 中编写一个简单的递归函数。在学习 OO Java 时,我也想编写涉及对象的 Python 代码。下面是我的代码。我提示用户输入一个数字,屏幕应该显示每个小于 5 的整数。

class Recursion:
    @staticmethod
    def recursive(x):
        if (x>5):
            print (x)
            recursive(x - 1)

def main(self):
    x = int(input('Enter a number for recursive addition: '))
    recursive(x)
Run Code Online (Sandbox Code Playgroud)

但是,当我在终端上运行它时,它会显示:“NameError: name 'recursive' is not defined”。这是错误的样子:

Python 3.5.1 (v3.5.1:37a07cee5969, Dec  5 2015, 21:12:44) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from Recursion import *
>>> a = Recursion()
>>> a.main()
Enter a number …
Run Code Online (Sandbox Code Playgroud)

python oop recursion nameerror python-3.x

5
推荐指数
1
解决办法
8047
查看次数

Python中命名的字符串格式参数

我正在使用Python 2.x,并尝试使用命名参数来理解字符串格式的逻辑.我明白:

"{} and {}".format(10, 20)打印'10 and 20'.

以类似的方式'{name} and {state}'.format(name='X', state='Y')打印X and Y

但为什么这不起作用?

my_string = "Hi! My name is {name}. I live in {state}"
my_string.format(name='Xi', state='Xo')
print(my_string)
Run Code Online (Sandbox Code Playgroud)

它打印 "Hi! My name is {name}. I live in {state}"

python python-2.7

5
推荐指数
1
解决办法
686
查看次数

java - 我们可以有一个弱线程吗?

我们可以对正在运行的线程进行弱引用,以便在低性能CPU中终止吗?

我的意思是,做这样的工作吗?

WeakReference<Thread> ref = new WeakReference<Thread>(new Thread(){
    public void run(){
        while(true){ /* your code */ }
    }
});
Run Code Online (Sandbox Code Playgroud)

这样,我想要一个线程具有低优先级来执行,我的意思是当CPU性能为低并且CPU完全使用时,线程的执行会自动终止.

有些线程不具有高优先级,应该以低CPU性能中断和终止.

java multithreading weak-references

5
推荐指数
1
解决办法
286
查看次数

hibernate hql-执行更新查询后返回更新的行ID列表

我在Java spring MVC应用程序中将Hibernate Query Language(HQL)与Oracle数据库一起使用。我以这种方式编写了一个HQL更新查询:

String hql = "update " + MyModel.class.getName() + " e set e.field = " + value + " where ..."
//Acquiring session
...
Query query = session.createQuery(hql);
int result = query.executeUpdate();
Run Code Online (Sandbox Code Playgroud)

executeUpdate()方法返回更新的行数。但是我想在执行更新查询后获取更新行的ID的列表。HQL有什么方法可以做到这一点吗?

java oracle hibernate hql

5
推荐指数
1
解决办法
1513
查看次数

Python 变量和函数注释引发 AttributeError

尽管变量current_timestamp(因此也是函数返回值)和变量timedatetime.datetime类对象,但以下代码引发 AttributeError: type object 'datetime.datetime' has no attribute 'datetime'

from datetime import datetime

def current_time() -> datetime.datetime:
   
    current_timestamp: datetime.datetime = datetime.now()
    return current_timestamp

time: datetime.datetime = current_time()
print(time)
Run Code Online (Sandbox Code Playgroud)

仅更改函数返回值和time变量的注释即可解决问题。

from datetime import datetime

def current_time() -> datetime:
   
    current_timestamp: datetime.datetime = datetime.now()
    return current_timestamp

time: datetime = current_time()
print(time)
Run Code Online (Sandbox Code Playgroud)

谁能解释为什么注释datetime.datetime只为函数返回值和time变量引发 AttributeError而不是为current_timestamp变量引发?

我正在运行 Python 3.8。

python python-3.x

5
推荐指数
2
解决办法
74
查看次数